kbdavis

Add Command in Context Menu?

29 posts in this topic

I need help adding a command to print the directory to the context menu (the right-click menu).

I have created a .bat file in the windows directory with the following information:

@echo off
dir %1 /-p /o:gn > “%temp%\Listing”
start /w notepad /p “%temp%\Listing”
del “%temp%\Listing”
exit

Now I just need to figure out the option to execute this .bat file by right-clicking in the directory I want to print.

Any ideas?

Edited by kbdavis
0

Share this post


Link to post
Share on other sites

Take a look in HKEY_CLASSES_ROOT\Directory\shell (or similar) for an example to copy.

If the batch file is named printdir.bat, it might look something like this:


REGEDIT4

[HKEY_CLASSES_ROOT\Directory\shell\Print]
@="&Print Directory"

[HKEY_CLASSES_ROOT\Directory\shell\Print\command]
@="C:\\WINDOWS\\printdir.bat \"%1\""

Edit: Added missing "REGEDIT4" to sample for clarity.

Edited by jumper
0

Share this post


Link to post
Share on other sites

Right now I can't find an older post I made about this, but I was researching this a while back. There are two main methods ...

[HKEY_LOCAL_MACHINE\Software\Classes\Directory\Shell\xxx]

[HKEY_LOCAL_MACHINE\Software\Classes\Folder\Shell\xxx]

It looks to me that using FOLDER is preferable because I am pretty sure that DIRECTORY is not processed when right-clicking hidden/system folders.

Furthermore, I noticed that on Win9x, when using both methods they appear consecutively in the list. By inserting a label into dummy entries I was able to get horizontal lines to show up directly inside the right-click menu, and this is roughly how Win9x displayed it ...

===[ BEGIN Shell:Directory ]==================

item

item

item

===[ END Shell:Directory ]==================

===[ BEGIN Shell:Folder ]=====================

item

item

item

===[ END Shell:Folder ]=====================

However, on WinXP, so far the label for DIRECTORY does not show up, but the label for FOLDER does. Interestingly, the items are co-mingled together. I will need more time to experiment though. Consider these preliminary observations. I'll try to get some screenshots next time.

0

Share this post


Link to post
Share on other sites

Take a look in HKEY_CLASSES_ROOT\Directory\shell (or similar) for an example to copy.

If the batch file is named printdir.bat, it might look something like this:


[HKEY_CLASSES_ROOT\Directory\shell\Print]
@="&Print Directory"

[HKEY_CLASSES_ROOT\Directory\shell\Print\command]
@="C:\\WINDOWS\\printdir.bat \"%1\""

I attempted this method, also using the other poster's method with the "folder" instead of directory, with no success. Any troubleshooting ideas?

*By the way, thanks for the quick response for both posters!

EDIT:

Please note that your method may work, I just have limited knowledge of editing the registry and I may have made the error with the execution part.

Edited by kbdavis
0

Share this post


Link to post
Share on other sites
I attempted this method, also using the other poster's method with the "folder" instead of directory, with no success. Any troubleshooting ideas?

Please post the exact registry scripts you tried. Usually it is just a matter of getting the quotes and backslash characters "escaped" correctly. This just means using the proper syntax so that REGEDIT actually inserts the expected string into the registry.

0

Share this post


Link to post
Share on other sites
I attempted this method, also using the other poster's method with the "folder" instead of directory, with no success. Any troubleshooting ideas?

Please post the exact registry scripts you tried. Usually it is just a matter of getting the quotes and backslash characters "escaped" correctly. This just means using the proper syntax so that REGEDIT actually inserts the expected string into the registry.

I attached a couple of screen shots to show you how I applied the registry edits.

post-374731-0-93074000-1363064191_thumb.

post-374731-0-53653100-1363064198_thumb.

0

Share this post


Link to post
Share on other sites
I attached a couple of screen shots to show you how I applied the registry edits.

It's exactly like I said, the error is in the REG script ( or you manually entered the REG script text ). The 2nd left photo clearly shows the error right away with the doubled backslashes and other stuff.

For those to exist as they appear in the photo, you either literally entered the escaped ( double backslash ) strings from the REG text file into the registry via the REGEDIT dialog boxes, or, your imported REG text file actually has quadruple backslashes "\\\\". If you were to now export that key you will see them.

Anyway, this is what you now should import into REGEDIT ( don't copy/paste the lines into REGEDIT! ) ...

REGEDIT4

[-HKEY_CLASSES_ROOT\Directory\Shell\Print]

[HKEY_CLASSES_ROOT\Directory\Shell\Print]

@="&Print Directory"

[HKEY_CLASSES_ROOT\Directory\Shell\Print\Command]

@="\"C:\\Windows\\Printdir.bat\" \"%1\""

I repeat, that chunk of text should be saved as a .REG file and then imported ( or dropped onto ) REGEDIT.

P.S. I would personally use "FOLDER" instead of "DIRECTORY".

EDIT: correction, I meant the 1st photo has the incorrect \Command] strings.

Edited by CharlotteTheHarlot
0

Share this post


Link to post
Share on other sites

Thanks for the tip and the quick reply. The registry edit is working now.

Now I have a new problem

See the attached screenshot of the issue. We know that the registry tweak worked because it directed to the PrintDir.bat file and actually managed to open notepad as instructed. So, I believe my issue now lies in the instruction block of the PrintDir.bat file. Below I have put in the code of the file, and would like to know if you know a way to tweak this the proper way...

@echo off
dir %1 /-p /o:gn > “%temp%\Listing”
start /w notepad /p “%temp%\Listing”
del “%temp%\Listing”
exit

Thanks for all the help, we are getting somewhere now!

post-374731-0-67664600-1363148029_thumb.

0

Share this post


Link to post
Share on other sites

Remove the angled double-quotes. This works for me:


@echo off
dir %1 /-p /o:gn > %temp%\Listing
start /w notepad /p %temp%\Listing
del %temp%\Listing
exit

Use normal double-quotes if your temp path has a space in it (mine doesn't).

Tip: don't use @echo off when debugging!

Also: I second the recommendation to try using "Folder" instead of "Directory" again.

0

Share this post


Link to post
Share on other sites
Thanks for the tip and the quick reply. The registry edit is working now.

Now I have a new problem

See the attached screenshot of the issue. We know that the registry tweak worked because it directed to the PrintDir.bat file and actually managed to open notepad as instructed. So, I believe my issue now lies in the instruction block of the PrintDir.bat file. Below I have put in the code of the file, and would like to know if you know a way to tweak this the proper way...

@echo off
dir %1 /-p /o:gn > “%temp%\Listing”
start /w notepad /p “%temp%\Listing”
del “%temp%\Listing”
exit

Some suggestions ...

(0) Personally I prefer a hardcoded path for the temp folder because the global %temp% variable can easily get changed either by accident or malware.

(1) Likewise with temp filenames, I also prefer filename.ext rather than just filename to avoid a possible directory name collision. But that's just me.

(2) For absolute precision I prefer %1\*.* instead of just %1 because I don't trust the shell ( context menu ) to supply the trailing backslash. Indeed I just checked on WinXP and it does not supply the backslash. So if a file exists that matches the name of the directory you are right-clicking there will be a collision and instead of getting a directory listing you will get the name and size of the file. Also note, as jumper mentioned, there is the possibility of spaces in the folder name. But in the example registry string I gave you above, it is already handled by the escaped string ... @="\"C:\\Windows\\Printdir.bat\" \"%1\""

(3) I don't think you need the colon ":" in the DIR command sort switch. Try using just ... /ogn

(4) You are using the reverse prefix "-" on the /p pause ( did you instead want it to be on the /o sort order? ) And I am confused why it should be paused when it is intended to be printed. Or, perhaps you have DIRCMD defaults and this is intended to reverse that and prevent pausing?

(5) You are using a temp file and then deleting it, thus you need the START /w which makes sense, but the whole process is unnecessary. Since you are using the overwrite redirection operator ">" ( rather than the append ">>" ), the file is essentially deleted anyway. Scrap that concept and forget the temp file even exists and just launch NotePad. When it closes you can ignore the temp file ( no need to delete it ) because it will get overwritten later. It also may prove helpful to be able to refer back to the exact file you printed if something looks awry in the printout.

(6) I wouldn't even bother with the @echo off since without it you might get some echoed debugging clues in the CMD window if you should ever wind up in a hang situation.

(7) I don't recall needing EXIT for context menu launched batch files, but I know that using CLS after a blank line is an old trick to close stubborn CMD windows. There is a linefeed after CLS also.

So I would try this one ( depending on what that /p DIR switch is all about ) ...

dir %1\*.* /ogn /-p > C:\Temp\Listing.txt
notepad.exe /p C:\Temp\Listing.txt

cls

EDIT: almost forgot, you may want to add /a to list all files ( including hidden ) ...

dir %1\*.* /a /ogn /-p > C:\Temp\Listing.txt

Also, I always try to limit the failure vectors and specify exact filenames ( e.g., notepad.exe rather than just notepad ) to rule out malware cases like notepad.bat. Probably the best thing is to hardcode to C:\Windows\Notepad.exe to be completely safe!

Edited by CharlotteTheHarlot
0

Share this post


Link to post
Share on other sites

It works perfect. Thanks for the help and the speedy replies!

0

Share this post


Link to post
Share on other sites
It works perfect. Thanks for the help and the speedy replies!

Glad you have it sorted out. You should consider posting it here in its final form for others that might want to use PrintDir from the context menu. Perhaps you can also test it on a Hidden and System folder as well.

P.S. You might want to carefully audit your registry for other instances of double backslashes. If they exist they can stop other programs from working. I just started a separate thread about such things over here.

0

Share this post


Link to post
Share on other sites

Ok here is the final product instructions, thanks to the help of CharlotteTheHarlot & Jumper for their contributions:

Open up notepad, copy and paste this code:

REGEDIT4

[-HKEY_CLASSES_ROOT\Directory\Shell\Print]
[HKEY_CLASSES_ROOT\Directory\Shell\Print]
@="&Print Directory"
[HKEY_CLASSES_ROOT\Directory\Shell\Print\Command]
@="\"C:\\Windows\\Printdir.bat\" \"%1\""

Name it anything as long as you change the extension from .txt to .reg

Open the reg file you just created and apply the changes to the registry.

Next

In a new notepad, copy and paste this code:

dir %1\*.* /a /ogn /-p > C:\Temp\Listing.txt
C:\Windows\notepad.exe /p C:\Temp\Listing.txt

cls

Save this file as PrintDir.bat. This needs to be saved in your windows directory.

Afterwards, I would restart your computer, mainly because I don't trust anything to get loaded in the memory (hence, the new registry change you just did). You can also try killing the explorer.exe process then starting it again.

Again, the credit for this goes to CharlotteTheHarlot & jumper. I am just here to condense all the suggestions they gave to make this happen. Thanks again for the help, and hope this thread helps others who are needing to print a directory.

0

Share this post


Link to post
Share on other sites

Does anyone know the Reg Key for the file Command Context Menu for files

I added this for folders and it works, thank you for the information on how to do it.

post-5386-0-60886200-1363547588_thumb.pn

I have the DeleteIt to work from the SendTo Folder but would like it on the context menu underneath the create shortcut

post-5386-0-29332200-1363548010_thumb.pn

Is it possible to have the icon appear with the Delete It on the context menu, like the Microsoft Security Essentials

0

Share this post


Link to post
Share on other sites

Since you mentioned displaying an icon I think you might be referring to "the other kind" of context menu entry. The two main methods that I know are ...

Context Menu using "Shell" ...

[HKEY_LOCAL_MACHINE\Software\Classes\xxx\Shell\yyy]

[HKEY_LOCAL_MACHINE\Software\Classes\xxx\Shell\yyy\Shell]

[HKEY_LOCAL_MACHINE\Software\Classes\xxx\Shell\yyy\Shell\Open]

[HKEY_LOCAL_MACHINE\Software\Classes\xxx\Shell\yyy\Shell\Open\Command]

@="Drive:\\Directory\\Filename.exe < parameters >" <--- requires careful escaping of parameters and quotes

Context Menu using "ShellEx" ...

[HKEY_LOCAL_MACHINE\Software\Classes\xxx\ShellEx\ContextMenuHandlers\yyy]

@="zzz" <--- usually a {CLSID}, this is a direct pointer to another registry key ...

[HKEY_LOCAL_MACHINE\Software\Classes\CLSID\zzz]

@="Some Description"

[HKEY_LOCAL_MACHINE\Software\Classes\CLSID\zzz\InProcServer32]

@="Drive:\\Directory\\Filename.dll"

"ThreadingModel"="Apartment"

xxx is a well-known shell object. This list is from the link mentioned below, but might not be complete ...

* ...................... All files

AllFileSystemObjects ... All files and file folders

Folder ................. All folders

Directory .............. File folders

Directory\Background ... File folder background

Drive .................. All drives in MyComputer, such as "C:\"

Network ................ Entire network (under My Network Places)

Network\Type\# ......... All objects of type #

NetShare ............... All network shares

NetServer .............. All network servers

network_provider_name .. All objects provided by network provider "network_provider_name"

Printers ............... All printers

AudioCD ................ Audio CD in CD drive

DVD .................... DVD drive (Windows 2000)

yyy of course is merely an arbitrary label sometimes ( :lol: ) used to help determine what this handler does.

The "Shell" method is a really simple way from the GUI to pass the target ( the object right-clicked ) to an external program with some rudimentary ability to define some parameters for the external program and then launch it. I think of it as roughly equivalent to a batch file. This method does not allow icons to appear.

The "ShellEx" method is complicated in that a handler DLL needs to exist. By using a canned set of code in a DLL they can offer many more procedures at runtime, including displaying of icons and sub-flyout menus, etc. Note that ShellEx itself has a variety of methods of interacting with the shell, "ContextMenuHandlers" is only one of them. There are others like "DragDropHandlers" or "PropertySheetHandlers". I don't have complete list at the moment.

From what you say it sounds like you have a Shell handler. So it will not be able to have an icon. However if this program "Delete It" includes a context menu DLL, it very well may have an icon embedded and merely needs to be registered.

Creating Shell Extension Handlers (Windows) ( MSDN )

P.S. I can't find it but there is a thread in the Win9x forum that explores a particular 3rd party ShellEx launched DLL that allows someone to define icons and a variety of other features in the context menu. Some of these things I mention above I also mentioned in that thread..

Edited by CharlotteTheHarlot
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.