SHELLVERB — Display or change file associations and shell verbs.

Syntax:
SHELLVERB /C /D:string /M:string /Q /R /S /T:ftype /U /V:verb /Z ftype=command

/Creplace any safechars in command with ASCII
/D:stringdescriptive name for the file type; shown in Explorer
/M:stringdescriptive name for the verb, used in menus
/Qdo it quietly
/Rremove file association or shell verb
/Sdisplay or change system-wide settings (the default)
/T:filetypespecify the file type; only useful when ftype is an extension
/Udisplay or change per-user settings
/V:verbthe shell verb to display or change
/Zmake the specified verb the default
ftypemay be either an extension or a descriptive file type
commandthe command Windows should execute for the specified verb

This command can display or change file associations and “shell verbs” — the actions that Explorer can perform on a file. It can be used as a combination of the ASSOC and FTYPE commands, but it can also display and customize file types in ways that ASSOC and FTYPE cannot.

Windows Explorer uses a two-layer approach in determining how to work with a file. The extension (the final part of the filename, everything after the last period) is mapped to a file type, and the file type in turn has various actions associated with it. For example, a file named ReadMe.txt has the extension TXT, which maps to the file type “txtfile”. The file type txtfile then has certain actions defined — Open, Print. This command’s ftype parameter may be either an extension (beginning with a period), or else a descriptive file type. If ftype is an extension, SHELLVERB will automatically use the associated file type, or else create a new one.

Complicating matters further, Windows actually keeps multiple sets of file-association info: one set of system-wide file associations which applies to all users; and a per-user set of associations for each user, allowing him to override the system-wide settings. SHELLVERB can list or change either set. Specify /S to work with the system-wide file assocations, or /U to use the current user’s personal assocations. You can use both together. If you don’t specify either /S or /U, /S is the default.


To display the file type and actions associated with an extension, just type SHELLVERB followed by the extension. Note that the extension should begin with a period:

shellverb .txt

You can also use wildcards in the extension:

shellverb .htm*

If the ftype does not begin with a period, it’s interpreted as a file type instead. (Wildcards don’t work in this format.)

shellverb regfile


Notice that most file types have an “Open” verb. This verb is used to open, edit, or play a file, typically by double-clicking on its icon. You can use the =command syntax to set the command used to open a file. For example, if we want all .ZZZ files to open in Notepad:

shellverb .zzz=notepad.exe "%%1"

Note the use of "%1" as a placeholder for the filename in the command string. (Double the percent sign to prevent TCC’s variable expansion from expanding the variable reference before the SHELLVERB command gets it.)

You can set the command for other verbs with the /V: option:

shellverb /v:print .zzz=notepad.exe /p "%%1"

If you don’t specify /V:verb, the Open verb will be used by default. If you specify /V (without any verb), the default verb will be used; this is usually Open, but not always.


If you examine the .REG extension with

shellverb .reg

you’ll see that the Open verb has an alternate name: Mer&ge. I call this the verb’s “menu name”. This is generally done for one of two reasons: to give the command a more intuitive name (Merge instead of Open), or to set an accelerator key (the ampersand makes the G an accelerator key). You can set the menu name for a verb using the /M: option:

shellverb /v:Open /m:"&Edit" .zzz=notepad.exe "%%1"

creates an Open verb for .ZZZ files, which will appear as Edit (hotkey E) in the context menu.


The default verb, the one used when double-clicking on a file’s icon, is marked with an asterisk. Usually, but not always, this is Open. You can set the default verb with /Z:

shellverb /z /v:edit .reg

Now double-clicking on a .REG file will open it in Notepad, rather than trying to install it using RegEdit. /Z requires that you specify the verb to make the default with /V:; in fact you can combine the two options:

shellverb /z:edit .reg


You can set the descriptive name for a file type (shown in Explorer) with /D:string. Use /D: alone, without a string, to remove the descriptive name. Note that new or changed descriptions usually don’t take effect until Explorer restarts — after you reboot or log out.

shellverb /d:"Configuration file" .cfg


When ftype is an extension (i.e., when it begins with a period), SHELLVERB automatically supplies the appropriate file type, either using the file type already associated with that particular extension, or else creating a file type from the extension. For example, if you supply a ftype of .ZZZ and there is no file type associated with that extension, then SHELLVERB will create a new file type “zzzfile”. This is usually the desired behavior. However, on rare occasion you may want to associate an extension with some other file type; /T:filetype allows this. /T essentially replicates the functionality of the ASSOC command; however, you can combine it with other options to, say, associate an extension with a file type, set a description for the new file type, and define an Open verb, all in one command.

shellverb /t:TCC.Batch .btm


To remove an extension’s association with a file type:

shellverb /r .zzz

To remove a verb from a file type:

shellverb /r /v:print .zzz

To remove a file type altogether: (Warning — this deletes an entire tree from the registry, and cannot be undone!)

shellverb /r zzzfile

Note that SHELLVERB /R ZZZFILE deletes a file type and all the information associated with it (the description and all its defined verbs), while SHELLVERB /R .ZZZ merely breaks the connection between the extension and the file type. Usually the latter is to be preferred.

If you use /R to remove something, you will be prompted first. Use /Q to suppress the prompt and perform the action immediately. /Q also prevents the report that you get after making other changes.


If you specify /C, any characters in the command in the range U+FF01 through U+FF5E will be replaced with ASCII equivalents. For example, you can include an %@CHAR[0XFF05] in the command, and /C will translate it to a percent sign. This can be handy when copying shell verbs using the @SHELLVERB function.


Putting it all together, we can define a new file type for .BTM files, describe it, and define some basic shell verbs:

rem  Create a new file type for .BTM files:
shellverb /t:btmfile /d:"Take Command batch file" .btm

rem  Create an Open verb to run .BTM files:
shellverb /m:"R&un" .btm="%_cmdspec" /c "%%1"

rem  Create an Edit verb to edit them like .TXT files:
shellverb /c /v:Edit .btm=%@shellverb[.txt,open,128]

rem  Create a Print verb to print them using Notepad:
shellverb /v:Print .btm=notepad.exe /p "%%1"

rem  If BDEBUGGER is available, create a Debug verb:
iff isinternal bdebugger then
    shellverb /v:Debug /m:"Debu&g" .btm="%_cmdspec" /c bdebugger "%%1"
endiff


See also: the @SHELLVERB function, which returns the command string for the given shell verb.