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


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 off
If \{%2\}

\{\} @echo Syntax: AlterDefaultAction Type Default&goto :EOF setlocal set OK=N set type=%1 set action=%2 for /f "Tokens=1,2 Delims

" %%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=N if /i "%ntype%" EQU "Directory" set key=HKCR\Directory\shell&goto setnone if /i "%ntype%" EQU "Drive" set key=HKCR\Directory\shell&goto setnone if /i "%ntype%" EQU "Folder" set key=HKCR\Folder\shell&goto setnul for /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 setnul if /i "%action%" EQU "NONE" goto setnone for /f "Tokens=1,4 Delims=\" %%a in ('reg query %key%^|FIND "HKEY_CLASSES_ROOT"') do ( if /i "%%b" EQU "%action%" call :doit %%b ) :finish if "%OK%" EQU "N" @echo AlterDefaultAction did NOT set %type% to %action% endlocal goto :EOF :setnone set action=none call :doit none set OK=Y goto finish :setnul set action=nul @echo reg delete "HKCR\Folder\shell" /ve /F call :nulq>nul 2>&1 set OK=Y goto finish :doit @echo reg add %key% /VE /T REG_SZ /F /D %1 reg add %key% /VE /T REG_SZ /F /D %1 set OK=Y goto :EOF :nulq reg delete "HKCR\Folder\shell" /ve /F



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish