Right Click, command prompts, batch files and all that jazz…..

Right clicking a folder or file in Windows Explorer presents a context menu which can be modified by programs or the user. I read a lot of posts on how to remove or disable items in the right click menu but not how to add an item.

This started with wanting to generate a list of files in a directory as a text file. This led me to a number of options including a how to on this with a right click menu option to launch it at the Elder Geek site.

This is the content of my filelisting.bat:
cd %1
dir /a /b /-p /o:gen >List_Files.txt
start notepad List_Files.txt

But this actually does not work as described. The action of right clicking on a directory generates a file list ok, but only of the contents of the containing directory not the selected directory. In other words it does not drill into the selected directory.

There is also an issue with Windows XP being different to Windows 7 as the end user interface to add items to the right click menu has been removed in Windows 7.

A lot of links recommend http://www.nirsoft.net/utils/shell_menu_new.html which I downloaded but I could not see or read how to Create a new Item with the tools only how to remove or disable options.

So I resorted to editing the registry directly and read a number of articles on how this could be done. The one at Tech Recipes was helpful.

The Microsoft listing of environment variables is a useful reference.

Eventually I wound up at Microsoft Support Article 321379 “How to add the Print Directory feature for folders in Windows XP, in Windows Vista, or in Windows 7” which is referenced by many other sites but not explained by any that I looked at.

The registry entries as described by Microsoft I reviewed as follows:

Windows Registry Editor Version 5.00

Must appear as the first line or the file will not be recognised as a registry merge file.

[HKEY_CLASSES_ROOTDirectoryShell]
@=”none”

This is simply setting up the container key in the registry. In my case it already existed from something else I had installed.

[HKEY_CLASSES_ROOTDirectoryShellCreate_File_Listing]
@=”Create File List”

This is the text that will appear in the right click menu when the context is a Directory.

[HKEY_CLASSES_ROOTDirectoryshellCreate_File_Listingcommand]
@=”filelisting.bat “%1″”

This is the command that will be executed when the right click menu item is selected (clicked on).

These three by themselves provide the facility that I needed so I did not create the rest of the entries. I’ve included them here for completeness and one day might get around to understanding what they do.

[HKEY_CLASSES_ROOTSOFTWAREClassesDirectory]
“BrowserFlags”=dword:00000008

Not sure what this does……

[HKEY_CLASSES_ROOTSOFTWAREClassesDirectoryshellCreate_File_Listing]
@=”Create File List”

This appears to repeat the previous menu name option. Removing the registry entry above and adding this one by itself does not display in the context menu. It might be redundant or might relate to the per-user class registration process described by Microsoft.

[HKEY_CLASSES_ROOTSOFTWAREClassesDirectoryshellCreate_File_Listingcommand]
@=”filelisting.bat “%1″”

[HKEY_CURRENT_USERSoftwareMicrosoftWindowsShellAttachmentExecute{0002DF01-0000-0000-C000-000000000046}]
@=””

[HKEY_CLASSES_ROOTSOFTWAREClassesDirectory]
“EditFlags”=”000001d2”

So now I had a working command structure in the right click context menu. But I could not work out how to get it to work unless I placed the filelisting.bat file in the c:windows directory. I tried using the full path to the file in my usersdocumentsbin directory which is where I store my .bat files and similar stuff (mainly so I can backup my specials and move them to a new PC easily) but no matter what I tried it refused to work unless the file was in the c:windows directory, so that is where it is.

Once the file would launch I could demonstrate that I now had the child directory parameter being passed to the bat file and I could generate the file list within the selected directory rather than it’s parent.

Other sites I referenced included:

  • http://commandwindows.com/batchfiles-iterating.htm
  • http://spreadsheetpage.com/index.php/tip/getting_a_list_of_file_names/
  • http://www.ericphelps.com/batch/lists/filelist.htm
  • http://www.tomshardware.com/forum/90764-45-calling-batch-file-context-menu-_multiple_-param
  • http://www.tech-recipes.com/rx/232/right-click-to-open-a-command-prompt/
  • http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/cmd.mspx?mfr=true

One reply

  1. I know this post is old, but this might help someone
    Forget the .bat file use:

    Cmd /c CD “%L” && dir /a /b /-p /o:gen >List_Files.txt && start notepad List_Files.txt

Leave a Reply to Tdude Cancel reply

Your email address will not be published. Required fields are marked *