Metareven
Metareven

Reputation: 842

adding a shell context menu item so that I can add folder to path by right clicking

Often at work I have to install new frameworks etc which do not add themselves to path and I have to go through the tedious process of adding the exectuables to path. I therefore decided to add a shell context menu item so that I can add any given folder to the path just by right clicking it and selecting "add to path".

I went through the normal routine of creating a context menu item and I used the following command to add the folder to path:

setx PATH "%PATH%;%1%"

This seems to not evaluate the PATH-variable, and instead replaces my PATH with something like this:

PATH;C:\Program Files (x86)\Android\android-sdk\platform-tools

Is there a way to make the context menu item evaluate %PATH% instead of just ignoring the percentage signs? I've read about using \,^ and just adding an additional % but none of these approaches seem to work.

In case it matters, this is on a Windows 7 Enterprise computer

Upvotes: 1

Views: 2526

Answers (3)

LaundryHamper
LaundryHamper

Reputation: 1

9 years late, but I found this while trying to figure it out myself earlier - I gave up but then thought of a stupid workaround.

Make a folder (I called mine PAFF, innit), open system properties, add it to PATH. While you're there, under system variables (rather than user variables), edit PATHEXT and add .lnk to it (...or do SET PATHEXT=%PATHEXT%;.LNK).

Create a shortcut to the folder and put it in %appdata%\Roaming\Microsoft\Windows\SendTo.

Now you can right-click > send to > PAFF. It will put copies of things there rather than shortcuts, so if you send a folder, https://i.imgur.com/l20bTR2.jpeg. To send a shortcut, create shortcut first, then send to PAFF the shortcut. Shortcuts to .exes in PATH work how you'd want them to, no extension needed, so the files themselves can stay wherever they are and cmd won't have to check what's in a million folders when it starts - this is also good because I can't figure out a way to cd to the target of a shortcut. You can send to PAFF a shortcut to a folder and open it by specifying "thing.lnk", including the quotes, but that just opens it in explorer and that seems to be it. Maybe if there's a streamlined right-clicky way to make symlinks, that could be the route to a lazy solution? I'm not pushed to figure it out, the executables are enough for me.

Opening cmd and having C:\Windows\system32>eldenring just work is pretty swag.

Upvotes: 0

alchemy
alchemy

Reputation: 982

Wowwwwww! I just spent the last 6+ hours of my life trying to be able to add a directory to my path (permanently) from the context menu. Well done Windows!

nircmd.exe elevate "cmd.exe" /k "setx /M PATH %%PATH%%;%1" && exit

Big thanks to @Metareven for some crucial bits (the double %s). Failed a few years back. Links below for related info and hopefully a reg file. AddToPath.reg

Wiped all my paths in the process! Totally worth it! :)

You need nircmd.exe in your C:\windows\system32 folder (or in your path!). The "/k" is necessary for god only knows why. "/M" is for machine, for system, for permanente. (I was like two attemps from giving up and wasting all these hours).

Use RapidEnvironmentEditor (in admin mode) to check, the cmd prompt that opens will not have the current PATH info. Get double ;s for some reason. Still doesn't work by reg file below (anyone know why??) You'll have to use regedit or AdvancedRegistryEditor to make an entry manually (see link below). Use EcMenu.exe to erase context menu mistakes (and other crud).

Windows Registry Editor Version 5.00  **doesn't work**

[HKEY_CLASSES_ROOT\Folder\shell\AddToPath]
"Add To Path"

[HKEY_CLASSES_ROOT\Folder\shell\AddToPath\command]
nircmd.exe elevate "cmd.exe" /k "setx /M PATH %%PATH%%;%1"

This did actually work for me (without the double %s) but only to User PATH:

cmd /k setx PATH "%PATH;"%1 && pause

How add context menu item to Windows Explorer for folders

How to Run a Program Elevated via the Right-Click Menu in Windows ...

This might also work instead of nircmd somehow: https://superuser.com/a/938121 "C:\Windows\System32\cmd.exe"="~ RUNASADMIN"

Tried this, didn't work: https://superuser.com/questions/266974/any-freeware-program-for-adding-editing-path-from-context-menu

Upvotes: 1

Metareven
Metareven

Reputation: 842

Managed to find a permanent solution. Since setx sets the user path and not the system path, the command mentioned in my question will add all elements in the combined userpath + system path to PATH, effectively doubling its size every time you run the script.

This can either be fixed by removing user path, or as I did, add another user variable and append that to path. I then ended up with the following script afterwards to set the path correctly:

cmd /k setx UPATH "%%UPATH%%;%1%" && exit

This way I don't need to use a bat-file. Using double %s and &s seem to work as a way to escape the character, thus making it look like this to cmd:

setx UPATH "%UPATH%;drive:/theFolderYouRightClicked" & exit

I am still not sure why you have to pass this through cmd in order to see the PATH-variable, but at least this is a semi-clean way of solving my problem

Upvotes: 1

Related Questions