Suppose my PATH is C:\WINDOWS\system32\;C:\Program Files\Important\
SET NEW_PATH=C:\My\Dir\
SETX PATH "%PATH%;%NEW_PATH%"
Results in a path of:
C:\WINDOWS\system32\;C:\Program Files\Important\;C:\My\Dir"
Notice the quotation mark at the end of the path. It's as though the backslash at the end of %NEW_PATH% escaped the final quote mark. I need the quotation marks because I have spaces in my path, but I don't want backslashes to be interpreted as escape characters.
What's the right way to include my PATH in the call to SETX?
Don't put a backslash at the end of
%NEW_PATH%
.If you got random input to begin with instead of stuff you control, then you can do one of the following:
Leave out the closing quotation mark. This does work in some cases:
But doesn't do so well in others:
However, you have that problem already to begin with, so what you did there was dangerous at best, since it would destroy quoted paths (which you need if your path includes a semicolon).
Which leads us to the following option:
Properly escape whatever goes into the command:
If
NEW_PATH
does not contain spaces, then you do not need to quote it:If
NEW_PATH
does contain spaces, you could use the literal value, and again not quote the final backslash: