SgDotNet
Singapore Professional .NET User Group -For Cool Developers

Windows Explorer Context Menu, SentTo, and Open Command Window Here

Context menu could be very handy at times but overdoing is sometimes detrimental, in terms of responsiveness.

It is useful to note down several frequent registry locations that can speed up adding, removing or locating them.

// Global type
HKEY_CLASSES_ROOT\*\shell\[name]\command
@="[command]"

HKEY_CLASSES_ROOT\*\shellex\ContextMenuHandlers\[name]
@="[GUID]"

// Specific
HKEY_CLASSES_ROOT\[file type]@="[friendly name of file type]

HKEY_CLASSES_ROOT\[file type]\shell\[name]\command
@="[command]"

HKEY_CLASSES_ROOT\[file type]\shellex\ContextMenuHandlers\[name]
@="[GUID]"

HKEY_CLASSES_ROOT maps either HKEY_LOCAL_MACHINE\SOFTWARE\Classes or HKEY_CURRENT_USER\SOFTWARE\Classes.

Popular [file type] are: dllfile, exefile, Folder, Directory, Drive.

The most useful one to add is probably being able to right-click on a folder and open its path in the command prompt with:

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\cmd]
@="Open Command Window Here"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\cmd\command]
@="cmd.exe /k \"cd %L\""


The limitation of the above is that if you want to start the command prompt in the directory where a particular file is in, you have to go up one level first and right click on the parent folder to start it -- this is not very convenient.

I chose to put the functionality into the SendTo folder instead. Create a shortcut in the "C:\Documents and Settings\[user]\SendTo" folder, name it as "cmd line here" and in the target window enter "%windir%\System32\cmd.exe /k echo".

You will find out that if your right click on a directory or a file and use "SendTo\cmd line here", the current directory or file name is printed on the command line. This is not very useful but it is a terrific starting point.

I create a batch file with the following lines:

@echo off
rem accepting "drive:/path/dir/file" or "drive:/path/dir"
%~d1
cd %1 2> nul
if not errorlevel 1 goto end
cd %~dp1
:end

and save it as "cmdhere.bat" into one folder which must be on the PATH, eg, C:\Tools.

Edit the target of the created "cmd line here" to be "%windir%\System32\cmd.exe /k cmdhere.bat". Use it -- great, now you can either right click a folder or a file inside it to open the command prompt with SendTo -- the folder or file name has been passed onto cmdhere.bat as %1.

Command Extensions are enabled by default and must be ON.

Brief explanation (using // rather than REM):

cmdhere.bat makes use of "variable substitution with modifier".

// %~d1 expands %1 to a drive letter only and change drive
// required if the target folder is not on %HOMEDRIVE%
%~d1
// test if %1 is a directory
cd %1 2> nul
// no error means yes, end of story
if not errorlevel 1 goto end
// otherwise, %~dp1 expands to a drive letter and path only,
// thus change to the parent directory
cd %~dp1
:end

The possibilities are simply unlimited for any repetitive task.

Posted Nov 20 2004, 10:07 PM by blackinkbottle
Filed under:
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems