Windows: How to add batch-script action to Right Click menu

18

12

I have a few programs that creates temp files or backup files or similar files that are not important. For example, GVim for Windows by default creates a backup file in filename.txt~.

I sometimes need to clean up a directory and remove all these files. I have made a simple .bat file for this. However, it is cumbersome to have to start up CMD, navigate to the folder, run the script. Especially since this is a script that I would like to run often on various folders. And I do not want to copy the script to multiple folders, as this would be a maintenance nightmare.

So, I was thinking, that the best solution would be to add a Right Click menu item that allows me to run the script. So that I can right click on a folder in Explorer and click Cleanup and then have my script run on this folder.

So my question is: How do I add a right click menu action that runs a custom batch script?

ervingsb

Posted 2012-07-04T06:45:23.637

Reputation: 283

Answers

18

I have tried on Windows XP SP3 with this .reg key. Don't have Windows 7 at the moment to test it properly but it should be almost the same.

  1. Open notepad and paste the code from below.
  2. Edit as per your need.
  3. Save as MyScript1.reg
  4. Double click to import in registry.
  5. Test by Right click on any directory in Explorer
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\MyScript1]
@="Execute MyScript1"

[HKEY_CLASSES_ROOT\Directory\shell\MyScript1\command]
@="\"C:\\MyScriptsDirectory\\MyScript1Directory\\MyScript1.bat\" \"%1\""

Robert Schmidt

Posted 2012-07-04T06:45:23.637

Reputation: 423

Here's an answer that talks about using the right-click Send To with files: http://stackoverflow.com/a/6854941/550712. That answer references this Microsoft article: http://support.microsoft.com/kb/310270.

– Mark Berry – 2014-07-02T17:31:01.063

4I've edited your answer a bit. "Windows Registry Editor Version 5.00" is mandatory 'header' for .reg files. Otherwise Windows will refuse to inject the contents into registry. – extremko – 2012-07-04T10:48:18.227

+1 for pointing that one out. I totally forgot about that when c/p code. – Robert Schmidt – 2012-07-04T11:49:24.220

1I tried this. Now I can rightclick directyly on a folder, but it does not work if I rightclick inside a folder. Which is actually what I want. How to do that? – ervingsb – 2012-07-09T19:49:38.617

1That's a different question than the "So that I can right click on a folder in Explorer and click Cleanup and then have my script run on this folder." – Robert Schmidt – 2012-07-10T09:29:25.177

Yeah, that is why I clarified it. Sorry for being imprecise. I actually think of it as right clicking on the folder. It is just not the folder icon, but inside the folder. – ervingsb – 2012-07-12T08:40:06.757

20

Actually, the current answer isn't out of date. I tried the exact same thing on Windows 10 and was able to add Run Batch script to the context menu of all folders in Windows.

This is the content of my batch script (won't work with UNC paths):

@ECHO OFF
ECHO %~n0 was called with the following arguments:
SET args=%*
IF NOT DEFINED args GOTO END
ECHO %*
:END
PAUSE

The registry changes I made can be replicated with this REG file:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\Run Batch script]
@="&Run Batch script"

[HKEY_CLASSES_ROOT\Directory\shell\Run Batch script\command]
@="\"H:\\BATCH_FILE_PATH\\context-batch.bat\" \"%1\""

This only adds a context menu item for all directories/folders in Windows. If you want it showing for each and every file instead, you can use this:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\Run script]
@="Run &script"

[HKEY_CLASSES_ROOT\*\shell\Run script\command]
@="\"H:\\BATCH_FILE_PATH\\context-batch.bat\" \"%1\""

Alternatively, you can add your batch script to the Send To item list by creating a shortcut to your batch script and placing it under %APPDATA%\Microsoft\Windows\SendTo (or enter shell:sendto into the address bar)

If you want your script to show in the context menu that appears when you right click on the empty space within a directory (directory background?) you can use the following REG file:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\Run Batch script]
@="&Run Batch script"
"Icon"="%SystemRoot%\\System32\\shell32.dll,71"

[HKEY_CLASSES_ROOT\Directory\Background\shell\Run Batch script\command]
@="H:\\BATCH_FILE_PATH\\context-batch.bat \"%V\""

You don't need the "Icon"="%SystemRoot%\\System32\\shell32.dll,71" line. It simply adds an icon to your context-menu that looks like this:

context menu icon windows

Vinayak

Posted 2012-07-04T06:45:23.637

Reputation: 9 310

Thanks! By the way, do you know how to show this context menu when I right click the empty space of the current folder (not only when I click the folder itself)? – Tom Brito – 2016-04-28T15:24:28.753

@TomBrito Please see my latest edit. – Vinayak – 2016-04-28T16:18:00.187

@Tom Brito: Modifying empty space context menu seems to be answered here: http://stackoverflow.com/questions/4902041/windows-shell-add-item-to-context-menu-when-click-on-blank-part-of-folder

– kreemoweet – 2016-04-28T16:19:15.303

@kreemoweet Thanks for linking that! However, I found out about this by analyzing Context Menu Editor.

– Vinayak – 2016-04-28T16:22:22.747

@kreemoweet I changed the path string for Directory\Background to include %V as that would pass the current directory to the script. However, I'm not really sure what it means. %W seems to work just as well. – Vinayak – 2016-04-28T16:49:59.683

Still not working the way I want, I did post here: http://stackoverflow.com/questions/36946581/adding-batch-script-to-windows-8-context-menu

– Tom Brito – 2016-04-29T20:03:07.487

@TomBrito I think you missed the last part of my answer. Anyway, I answered your question on StackOverflow. – Vinayak – 2016-04-29T20:28:06.503

7

I would recommend Default Programs Editor for this task. It is both more user friendly and arguably safer than editing the registry directly.

Choose Context Menu enter image description here

Select the extension you want to change. enter image description here

Choose Add... enter image description here

Give your command a Title, browse to the batch file and optionally pick an icon to represent your command. enter image description here

Save the context menu to the registry and you're good to go.

Xtremity

Posted 2012-07-04T06:45:23.637

Reputation: 297

I would like to run the batch on folders, not a file type. Looks like it doesn't help... – Tom Brito – 2016-04-28T15:42:31.717

This helped me, thanks for sharing. – Daniel Kaplan – 2019-07-04T18:21:27.963

Nice program. Thanks. – Laurie Stearn – 2019-10-01T15:14:47.257

0

@Vinayak,

If using the "send to" option, you would probably need to add the line cd /d %1 to the start of the batch command so that it runs under the target path.

my current batch file looks like this:

@echo off
cd /d %1    
del *.txt
pause

Thanks for the help, I happened to stumble upon this via google at the right time :)

Chris

Posted 2012-07-04T06:45:23.637

Reputation: 1

This is really a comment and not an answer to the original question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. Please read Why do I need 50 reputation to comment? What can I do instead?

– DavidPostill – 2016-04-29T08:35:15.347

@DavidPostill well, he didn't have a post of his own in this thread to comment on, prior to posting his answer.. And (supposing he wanted to say something worthy of being a comment), what could he do to comment? nothing presumably. – barlop – 2016-09-23T12:30:00.660

@barlop Answers are not a substitute for comments as you well know. One alternative is to propose an edit to the other answer ... but that would likely get rejected. – DavidPostill – 2016-09-23T12:34:24.897

@DavidPostill my point is merely that then he has no option. and you're not really clear about that fact in your original comment. In fact your original comment is suggestive of the idea that he has some options but he doesn't. – barlop – 2016-09-23T13:05:30.110

@barlop Ah. OK. I'll think about some better wording ... – DavidPostill – 2016-09-23T13:07:40.493

0

Alternative solution which I use on Windows 10 is adding script to one of folders in path variable, and then when you are in explorer use sequence:

  • ctrl + L - to start writing in search bar

  • type: cmd, then press Enter - to start cmd in current directory

  • run your script, by typing its file name

jabone

Posted 2012-07-04T06:45:23.637

Reputation: 11