JSI Tip 8992. How can I use a batch to set the default action that occurs when you double-click a file type?

Jerold Schulman

January 30, 2005

2 Min Read
ITPro Today logo in a gray background | ITPro Today


When you double-click an object that has a defined file type, Windows Explorer performs the default action.

You can use Windows Explorer to alter this behavior using:

1. Tools / Folder Options / File Types.

2. Selecting the file type.

3. Press the Advanced button.

4. Select an action in the Actions list.

5. Press Set Default.

6. Press OK.

7. Press Close.

In Alter .REG file default behavior, back on Windows NT 4.0, I altered the .REG file type (extension) to cause a double-click to edit the file, instead of using the default, Merge.

NOTE: When you right-click a .REG file, the default is bold on the context menu.

Using REG.EXE on Windows 2000, from the Windows 2000 Support Tools, or REG.EXE built into Windows XP, Windows Server 2003, and later operating systems, I have scripted AlterDefaultAction.bat to set the default action.

The syntax for using AlterDefaultAction.bat is:

AlterDefaultAction Type Default

Where Type is the File Type and Default is the default action.

Sample Usage:

To set the default action of a .REG file to edit:

AlterDefaultAction .reg edit

AlterDefaultAction.bat contains:

@echo offIf {%2}
<h1><a name="_echo_Syntax_AlterDefaultAction_Type_Default_amp_goto_EOF_setlocal_set_OK_N_set_type_1_set_action_2_for_f_Tokens_1_2_Delims" id="_echo_Syntax_AlterDefaultAction_Type_Default_amp_goto_EOF_setlocal_set_OK_N_set_type_1_set_action_2_for_f_Tokens_1_2_Delims">{} @echo Syntax: AlterDefaultAction Type Default&amp;goto :EOFsetlocalset OK=Nset type=%1set action=%2for /f "Tokens=1,2 Delims</a></h1>
" %%a in ('assoc %type%') do ( if /i "%type%" EQU "%%a" set ntype=%%b)if not defined ntype set ntype=%type%set work=%ntype: =%if "%work%" NEQ "%ntype%" set ntype=%type%set key=Nif /i "%ntype%" EQU "Directory" set key=HKCRDirectoryshell&goto setnoneif /i "%ntype%" EQU "Drive" set key=HKCRDirectoryshell&goto setnoneif /i "%ntype%" EQU "Folder" set key=HKCRFoldershell&goto setnulfor /f "Tokens=1,2 Delims==" %%a in ('ftype %ntype%') do ( if /i "%ntype%" EQU "%%a" set key=HKCR%ntype%shell)if /i "%action%" EQU "NUL" goto setnulif /i "%action%" EQU "NONE" goto setnonefor /f "Tokens=1,4 Delims=" %%a in ('reg query %key%^|FIND "HKEY_CLASSES_ROOT"') do ( if /i "%%b" EQU "%action%" call :doit %%b):finishif "%OK%" EQU "N" @echo AlterDefaultAction did NOT set %type% to %action%endlocalgoto :EOF:setnoneset action=nonecall :doit noneset OK=Ygoto finish:setnulset action=nul@echo reg delete "HKCRFoldershell" /ve /Fcall :nulq>nul 2>&1set OK=Ygoto finish:doit@echo reg add %key% /VE /T REG_SZ /F /D %1reg add %key% /VE /T REG_SZ /F /D %1set OK=Ygoto :EOF:nulqreg delete "HKCRFoldershell" /ve /F



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like