1

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

Metareven
  • 822
  • 2
  • 7
  • 26
  • Fixed it for now by running a bat-file instead which is able to evaluate percentages correctly. Also had to create another system variable (I called it UPATH) and make the script change that system variable instead of PATH. Got some strange issues with things being added twice to PATH if I tried to set it directly – Metareven Jul 07 '15 at 13:43

2 Answers2

1

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

Metareven
  • 822
  • 2
  • 7
  • 26
1

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

alchemy
  • 954
  • 10
  • 17
  • This answer has a lot of offtopic parts that don't answer the question. Please remove the parts that don't work. – Amin Ya Apr 17 '20 at 17:51