setx setting PATH with spaces
Asked Answered
E

7

6

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?

Eximious answered 13/6, 2012 at 8:39 Comment(1)
note that setx variables written to the local system are not available immediately (they are available if you relaunch cmd)Bobbybobbye
R
10

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).

Ronnironnica answered 4/2, 2015 at 22:41 Comment(2)
doesn't work for paths with unavoidable spaces like `%PROGRAMFILES%\MySQL\MySQL Server 5.7\bin`Tether
this worked in a .bat: setx /m HERE "%~dp0 (with spaces in the .bat path)Wyman
S
3

I am a bit late but I was looking for something similar and was unable to find it so I figured it out on my own. I was trying to set variable to launch VSC# from Command Line and the exe file for VSC# was present at a path containg space similar to your case. So the solution for me was to set variable using the following:

setx vcs "\"E:\Program Files\IDE\vcsexpress.exe\"
Stidham answered 30/7, 2016 at 23:22 Comment(0)
Y
2

Single quotes never work in Windows. Period.

Your code does work for me with the double quotes, actually.

Yehudit answered 13/6, 2012 at 8:49 Comment(1)
Perhaps I should add that I am on windows XP. The command works but some values in the PATH include files in Program Files, so at the first Program Files the path truncates. So the last thing in my PATH is \ProgramEximious
D
2

The correct format is setx var "\path1;\path\path2\" /M only NOTE that since my path ends with a slash, the net effect is that the closing " is escaped and added as part of the path.

If your path ends with a slash... you must escape the last backslash... the corrected version is then setx var "\path1;\path\path2\\" /M

The quotes are needed to protect any embedded spaces ..eg "program files"

Disruption answered 18/8, 2016 at 3:59 Comment(0)
P
1

I found this question because I was looking for something a little bit different. I need to put quotes into the thing I'm setting. This isn't exactly what the OP is asking about but may be related if the problem is embedded quotation marks.

setx SVN_EDITOR C:\Program Files\vim\vim74\vim.exe

I found that this, while "correct", doesn't work because the user of this variable gets tripped by the space in the path so I needed to put quotes in there.

I found that

set SVN_EDITOR="C:\Program Files\vim\vim74\vim.exe"

worked just fine so I thought I'd try the normal way of escaping quotes on Windows:

setx SVN_EDITOR "^"C:\Program Files\vim\vim74\vim.exe^""

but this didn't work, resulting in the error:

 ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).
 Type "SETX /?" for usage.

So I just opened system settings in the control panel and set the value in the environment variables GUI. Works fine now, I can type set svn at the command line and it shows quoted as I need it to be.

If you really need to set your environment variable programatically you'll need to look at banging on the registry yourself. This is pretty straight forward in both C and Python (and probably in other languages too). It doesn't seem as though setx can do it for you though.

Parsimony answered 21/8, 2015 at 19:47 Comment(0)
E
0

I abandoned trying to use the %PATH% directly as this had the problems mentioned above with the spaces in the Program Files.

I wrote a small command to replace "Program Files" with "Progra~" but quickly I realised that this was no good as there were other paths with spaces. Replace

I found a good post describing quering the registry.
So as a test I set a System variable AAA with the path and the gradle appended

for /F "tokens=2* delims=   " %%f IN ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path ^| findstr /i path') do set OLD_SYSTEM_PATH=%%g

setx.exe AAA "%OLD_SYSTEM_PATH%%%GRADLE_HOME%%\bin;" -m

Hope this helps

Eximious answered 13/6, 2012 at 12:59 Comment(0)
T
0

I've found the following to work with four leading spaces, and three trailing. Generally, Windows escapes a double quote with another double quote, so the trailing double quotes make sense, but the extra double quote at the front of the path is a bit strange.

setx VAR """"C:\path\to a file\with spaces\file.txt"""
Tarpley answered 22/1, 2020 at 18:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.