Joona
Joona

Reputation: 58

Get path where i used the Context Menu

I want to add an option to the Win10 Context Menu, which executes a batch file in the folder where I right-clicked. I created a Key for this in Regedit under "\HKEY_CLASSES_ROOT\Directory\Background\shell" and a subkey command. Under the command Key I added the command cmd /c "start C:\Users\me\Documents\bat\file.bat". This works fine, but it only executes the batch file in C:\Users\me\Documents\bat\ but I want it to execute the batch file in the folder where I right-clicked. I thought of copying the batch file to the folder where I want to execute it first, but I don't know how to get the path.

Upvotes: 1

Views: 1945

Answers (2)

Compo
Compo

Reputation: 38623

If you enter the following at the Command Prompt:

%SystemRoot%\System32\reg.exe Add "HKCU\SOFTWARE\Classes\Directory\Background\shell\Run file.bat\command" /VE /D "%ComSpec% /D /C \"Start \"\" /D \"%V\" \"%UserProfile%\Documents\bat\file.bat\"\"" /F

It should create a context menu entry for you, which should do as you've asked. i.e. create an entry which should look something like this:

C:\WINDOWS\system32\cmd.exe /D /C "Start "" /D "%V" "C:\Users\me\Documents\bat\file.bat""

When you right click in the background of your Explorer Window, you should see a context menu entry named Run file.bat, which should run your script file.bat, located in your \Documents\bat\ directory, and with the cuurrent working directory as that which you clicked in the background of.

There is absolutely no need to make changes to the content of file.bat

Please note that as the file was clearly located within your own profile, and because you shouldn't really be creating context menu entries for every user on your system, I used HKEY_CURRENT_USER as the root key for the data. If you really do have a need to add the entry to for all users, and want to still create it from cmd.exe, ensure your cmd.exe instance is 'Run as administrator', and change HKCU to HKLM. Please also note that you should not add entries directly in HKEY_CLASSES_ROOT, as that is really just mapped from other locations, The appropriate locations are HKEY_LOCAL_MACHINE\SOFTWARE\Classes HKEY_CURRENT_USER\SOFTWARE\Classes, as offered above.

Upvotes: 0

nevilad
nevilad

Reputation: 985

To execute your bat file in a directory, you dont have to copy it there, you can change the working directory.

Change your registry command to:

cmd /c "start C:\Users\me\Documents\bat\file.bat %w"

This will pass the menu folder as argument to your batch file. Add directory change at start of your bat file:

cd /d %1

Upvotes: 2

Related Questions