setx setting PATH with spaces

Shawn Vader picture Shawn Vader · Jun 13, 2012 · Viewed 8.4k times · Source

I am trying to update the system Path variable in a win32 shell script but only if a value is not present.

My current version looks something like this:

for %%f in (xyz.exe) DO if [%%~$PATH:f]==[]; setx Path "%PATH%;%GRADLE_HOME%\bin" -m

The problem I am having is with

setx Path "%PATH%;%GRADLE_HOME%\bin"  

This doesn't work, but if I change the quotes to single quote

setx Path '%PATH%;%GRADLE_HOME%\bin'

It does work but the Path ends at the first occurrence of Program Files. I.e abc;def;Program

Is there any way to overcome this?

Answer

Eric picture Eric · Feb 4, 2015

Actually there is an easier way.. it seems to work but it is somewhat ugly and non-intuitive:

setx /M Path "%PATH%;%GRADLE_HOME%\bin

Notice that there is ONLY one double-quote and it is at the beginning .. if you put one at the end, it ends up in the path string (without the one at the beginning).