Wednesday 27 April 2011

Temporary PATH Additions: Modifying the standard CMD Here Extension

A relatively common shell extension for Windows systems is to have right-click for folders in Explorer offer the option to open a cmd prompt window with that folder as the current working directory (CWD). I seem to have added this to any Windows installation that I've used for any period of time. This is often called "cmd here" or "command here" and Microsoft provide an installable for this function in their PowerToys collection.

In fact, this feature is so useful that it's built-in to OSes from Vista onwards, but to access it you need to hold "shift" while you right-click the item and it only works for folders (some versions of the extension let you right-click a file and have the command prompt open in the folder containing that file). 

Something I find useful from time to time (and which I've never seen elsewhere) is to have file and folder context menus open cmd windows with the folder concerned added to the PATH environment variable, just for that session. This is great for uncommon or temporary use of a folder containing one or more exes without permanently bloating your PATH variable. Some programs with command line interfaces (such as Visual Studio) provide Start Menu shortcuts that open a cmd window with PATH modified for that window only and what I'm suggesting here is similar (but more dynamic).

All that happens when installing the "cmd here" extension is the addition of a few registry entries, and so I made a typical version of this add-on (based on the entries in the Win 7 registry and this web page) and adapted it. Save the following lines as a .reg file and you can add this to your file / folder context menus too:
Windows Registry Editor Version 5.00

    [HKEY_CLASSES_ROOT\*\shell\pathhere]
    @="Cmd with &Path here"
    ;"Extended"=""
     
    [HKEY_CLASSES_ROOT\*\shell\pathhere\command]
    @="cmd /k path %W;%%PATH%% && pushd %%USERPROFILE%%"

    [HKEY_CLASSES_ROOT\Directory\shell\pathhere]
    @="Cmd with &Path here"
    ;"Extended"=""

    [HKEY_CLASSES_ROOT\Directory\shell\pathhere\command]
    @="cmd /k path %L;%%PATH%% && pushd %%USERPROFILE%%"

    [HKEY_CLASSES_ROOT\Directory\Background\shell\pathhere]
    @="Cmd with &Path here"
    ;"Extended"=""

    [HKEY_CLASSES_ROOT\Directory\Background\shell\pathhere\command]
    @="cmd /k path %V;%%PATH%% && pushd %%USERPROFILE%%"

The entries for \Directory\Background enable the same effect by clicking in the empty space in an Explorer window: you just get the current folder the window is displaying.

As an aside, Raymond Chen explains the difference between the \Folder and \Directory classes in this blog post. Note that what the registry (and Raymond) are referring to here as "directories" are called "file folders" in parts of the Windows UI. We have used the "Directory" branch because it makes no sense to have virtual folders as targets for this sort of extension.

You'll have noticed that each entry's root has a commented-out, empty string called, "Extended". If these are un-commented (and you're using Vista onwards), commands will be added only to the extended context menu, available with a Shift-right-click.

You could copy the \Directory\shell entry to the \drive\shell branch if you wanted to provide the same facility for drive roots. You may also want to specify a different CWD, and this is controlled by the appended "pushd" command. If you delete this (everything from the first ampersand onwards, but don't forget to retain the closing quotation mark), the CWD will be the folder in which the context-menu was opened.

One thing I haven't sorted out completely is the variable expansion. When context menu entries for files are activated, %D, %L and %V all hold the filename with its path and %W just holds the path; some other letters hold other cryptic values. The details for directories seem similar, but my tests for \directory\background consistently crashed Explorer. The MS implementation of "cmd here" in Win 7 uses %L for \directory and %V for \directory\background. I can't find any documentation listing all these variables and I'd be interested to know if anyone's come across any.

Usual disclaimers apply (although it's highly unlikely to do anything you don't want) – specifically, I haven't tested this on anything other than one Win 7 Pro 32-bit installation. However, I'd expect it to work pretty much across the board, although older OSes (XP / 2003 and previous) will probably ignore the "Extended" key.