Spinner Software Home About
KB Contact

Draft - Explorer Context Menus

Published: 10 Dec 2015 - Last update: 12 Dec 2015 (c) Nicolai Kjaer / Spinner Software B.V.

This document explains how to set up your own Windows Explorer context (right-click) menus.

This can be very helpful if you are frequently performing the same action on various files. You can write a command script to perform the actions you want ; the first commandline parameter will be the file to act on.

For example, I can right-click on any .msi file and run custom commands against that file. I've set up scripts to perform validation, generate reports, documentation, etc - all these options appear in the context menu, and I just choose which action to perform. Very handy!

Setup

Create a new folder that will hold your command scripts, and any additional files such as the setup batch files.

We will create two batch (bat, not cmd) files ; one for adding a single command, and one to add all commands.

addcmd.bat

@ECHO OFF
SETLOCAL

SET SRCDIR=%~dp0
ECHO Setting up context menu for cmd file %1

SET CMDFILEFULL=%1
SET CMDKEY=%CMDFILEFULL:~0,6%
SET CMDTEXT=%CMDFILEFULL:~7,-4%
SET CMDTEXTTRIM=%CMDTEXT:-= %
SET FORTYPE=%CMDKEY:~3%
ECHO Type = %FORTYPE%
ECHO Command = %CMDFILEFULL%
ECHO Key = %CMDKEY%
ECHO Text = %CMDTEXT%
ECHO Trimmed = %CMDTEXTTRIM%
SET CMDLINE=\"%SRCDIR%%CMDFILEFULL%\" \"%%1\"
ECHO Cmdline = %CMDLINE%

reg add HKCR\%FORTYPE%.Package\ContextMenus\Spinner\Shell\%CMDKEY% /f /t REG_SZ /v MUIVerb /d "%CMDTEXTTRIM%"
reg add HKCR\%FORTYPE%.Package\ContextMenus\Spinner\Shell\%CMDKEY%\command /f /ve /t REG_SZ /d "%CMDLINE%"

ENDLOCAL

setup.bat:

@ECHO OFF
TITLE Setting up explorer menus
SETLOCAL

ECHO Adding context menu
reg add HKCR\Msi.Package\shell\Spinner /f /t REG_SZ /v ExtendedSubCommandsKey /d Msi.Package\ContextMenus\Spinner
reg add HKCR\Msi.Package\shell\Spinner /f /t REG_SZ /v MUIVerb /d Spinner

REM Now add to the context menu:
FOR %%I IN (*.cmd) DO CALL addcmd.bat %%I

ENDLOCAL

We can now create all the commands we want, by simply creating a new .cmd file with a name in the format xxxccc-title.cmd, where xxx is simply unique numbering (001, 002, etc) and ccc is the type.

For example, 001msi-Hello.cmd would add to msi.package\contextmenus\spinner\shell\001\command.

TODO: Add base hkcr (.msi = Msi.Package) ; trim/simplify addcmd further.

(c) 2015 Nicolai Kjaer, Spinner Software B.V.