28

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?

clhy
  • 6,283
  • 8
  • 34
  • 66
ervingsb
  • 352
  • 1
  • 3
  • 10

6 Answers6

34

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
  • 10,625
  • 10
  • 54
  • 89
  • 1
    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)? – The Student Apr 28 '16 at 15:24
  • 1
    @TomBrito Please see my latest edit. – Vinayak Apr 28 '16 at 16:18
  • 1
    @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 Apr 28 '16 at 16:19
  • @kreemoweet Thanks for linking that! However, I found out about this by analyzing [Context Menu Editor](http://www.thewindowsclub.com/context-menu-editor-for-windows-7-vista-released). – Vinayak Apr 28 '16 at 16:22
  • @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 Apr 28 '16 at 16:49
  • Still not working the way I want, I did post here: http://stackoverflow.com/questions/36946581/adding-batch-script-to-windows-8-context-menu – The Student Apr 29 '16 at 20:03
  • 1
    @TomBrito I think you missed the last part of my answer. Anyway, I answered your question on StackOverflow. – Vinayak Apr 29 '16 at 20:28
  • It sets for all users. Can't we choose any entry to set it only for the current user? – Sandburg Sep 12 '22 at 08:26
  • 1
    @Sandburg According to [this answer](https://stackoverflow.com/a/334615/1768141), `HKEY_CLASSES_ROOT` is a merge of `HKEY_CURRENT_USER\Software\Classes` and `HKEY_LOCAL_MACHINE\SOFTWARE\Classes` so maybe you could create a key under `HKEY_CURRENT_USER\Software\Classes` and see if that works? – Vinayak Sep 12 '22 at 10:13
  • I get an error popup when i click my new context menu option: "This file does not have an app associated with it for performing this action..." any ideas? – Griffin Jun 14 '23 at 15:01
  • @Griffin maybe the file association settings for `.bat` files are invalid on your system. Could you try restoring them to default like this: https://superuser.com/questions/53948/how-do-i-restore-bat-files-association-with-the-system-make-them-run-when-doub – Vinayak Jun 15 '23 at 05:38
19

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\""
Alex Essilfie
  • 1,385
  • 2
  • 12
  • 23
Robert Schmidt
  • 433
  • 3
  • 11
  • 6
    I'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. – CommanderSix Jul 04 '12 at 10:48
  • 1
    +1 for pointing that one out. I totally forgot about that when c/p code. – Robert Schmidt Jul 04 '12 at 11:49
  • 1
    I 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 Jul 09 '12 at 19:49
  • 1
    That'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 Jul 10 '12 at 09:29
  • 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 Jul 12 '12 at 08:40
  • 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 Jul 02 '14 at 17:31
15

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
  • 407
  • 4
  • 7
1

@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 :)

Albin
  • 9,307
  • 11
  • 50
  • 89
Chris
  • 11
  • 2
  • 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](http://superuser.com/help/whats-reputation) you will be able to [comment on any post](http://superuser.com/help/privileges/comment). Please read [Why do I need 50 reputation to comment? What can I do instead?](https://meta.stackexchange.com/a/214174) – DavidPostill Apr 29 '16 at 08:35
  • @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 Sep 23 '16 at 12:30
  • 1
    @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 Sep 23 '16 at 12:34
  • @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 Sep 23 '16 at 13:05
  • @barlop Ah. OK. I'll think about some better wording ... – DavidPostill Sep 23 '16 at 13:07
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
  • 121
  • 1
  • 4
0

I am using one to add the registry entries when they do not exist, and use an icon file of the folder, if it exists, already renaming it to Folder.ico.

@echo off 

setlocal EnableExtensions
title <nul && title ..\\%~nx0
%__AppDir__%chcp.com 1252 >nul

set "_bat=%~dpnx0" & %__AppDir__%mode.com 40,02
set "_key=HKCR\Directory\shell\Set_Folder_Icon"

set "_reg_qry=%__AppDir__%reg.exe query "%_key%""
set "_ico_reg=%SystemRoot%\System32\shell32.dll,71"
set "_bat=%_bat:\=\\%" & set "_ico_reg=%_ico_reg:\=\\%"

>nul 2>&1 ( %__AppDir__%net.exe "session" && set "_adm=y" || set "_adm=n"
      %_reg_qry%|%__AppDir__%find.exe "_" && set "_reg=y" || set "_reg=n")

if /i "%_adm%|%_reg%" == "y|n" >nul 2>&1 (
    ( %__AppDir__%reg.exe add "%_key%" /ve /d "Set Folder Icon" /f
      %__AppDir__%reg.exe add "%_key%\command" /ve /d "\"%_bat%\" \"%%V\"" /f
      %__AppDir__%reg.exe add "%_key%" /v Icon /t reg_sz /d "\"%_ico_reg%\"" /f
    )&& goto %:^))else if /i "%_adm%|%_reg%" == "n|n" (set "_er=y" && goto %:^|
    )else if "%_adm%%_reg%" == "yy" (set "_er=n" && goto %:^|)else ;set "_er=n"
    
%:^)
2>nul cd/d "%~dpnx1\\." && %__AppDir__%Attrib.exe -r -a .\. || goto %:^|

if exist .\\Folder.ico (%__AppDir__%Attrib.exe -s -r -h +a .\\Folder.ico
    )else if exist .\\*.ico (for %%i in (.\\*.ico)do ren "%%~fi" Folder.ico & goto %:^/
    )else goto %:^| )

%:^/ 
if exist .\\Desktop.ini >.\\Desktop.old (
     %__AppDir__%Attrib.exe -s -r -h +a .\\Desktop.ini
     echo;[.ShellClassInfo] && echo;IconResource=Folder.ico,0
     %__AppDir__%Findstr.exe /bliv "\[\.Shell IconR IconF IconI" <.\\Desktop.ini
    )else >nul 2>&1 %__AppDir__%CertUtil.exe -decode -f "%~dpnx0" .\\Desktop.ini

if exist .\\Desktop.old >nul 2>&1 move/y .\\Desktop.old .\\Desktop.ini
        
%:^|
if "%_er%" == "y" (%__AppDir__%timeout.exe 5|echo\Admin rights required!
    )else if "%_er%" == "n" (%__AppDir__%Attrib.exe +s +r +h -a .\\Desktop.ini 
     <con: %__AppDir__%Attrib.exe -r +a .\. && %__AppDir__%Attrib.exe +r -a .\.
     <con: cd../ && %comSpec% /e:off/v:off/s/q/r "ren "%~dpnx1" "%~nx1" >nul 2>&1"
    )>nul 2>&1 else rem :: batch continue.
     
2>nul del/q /f /a "%~f1\Desktop.old" & endlocal & <con: move nul 2>&0 ||goto :eOf
::-----BEGIN -----Wy5TaGVsbENsYXNzSW5mb10gDQpJY29uUmVzb3VyY2U9LlxGb2xkZXIuaWNvLDA
NCltWaWV3U3RhdGVdDQpNb2RlPQ0KVmlkPQ0KRm9sZGVyVHlwZT1HZW5lcmljDQo=-----END -----::

This code considers:

  • That you are going to put the bat in the folder where it should be, where it will always be called.

  • Will run a first time (as an administrator) so that the necessary entries in the Windows registry are added, (by the bat itself) and pointed to the location of the/this File.bat.

  • The following executions will be right click menu, in the target folder

  • If a Folder.ico file is in the target folder, it will be used, in the absence, another icon file will be used, and renamed to Folder.ico

  • When a Desktop.ini file exists in the target folder, the necessary edits will be made to replace the .ico file, if not, a new one will be created.

  • To add or change a folder icon


  • You also can use a .cmd instead .reg file to add all entries in the Windows Register
  • Save the contents below as File.cmd, and run as an administrator to add the registry entries.
Windows Registry Editor Version 5.00 2>nul

;The following line/commands run and add the current file by "itself"
;@(cls & %__APPDIR__%reg.exe import "%~f0" >nul 2>&1 & goto=:EOF )

[HKEY_CLASSES_ROOT\Directory\shell\Set_Folder_Icon]
@="Set Folder Icon"
"Icon"="\"C:\\\\WINDOWS\\\\System32\\\\shell32.dll,71\""

[HKEY_CLASSES_ROOT\Directory\shell\Set_Folder_Icon\command]
@="\"F:\\\\2020-SU\\\\Q1635169\\\\Q1635169.cmd\" \"%V\""

; Just replace drive and folder\file name...

Obs.: Initially to answer

Io-oI
  • 7,588
  • 3
  • 12
  • 41