I want to modify the Windows PATH variable using setx
. The following works at least 50% of the time on Windows 8:
setx PATH %PATH%;C:\Python27\;C:\Python27\Scripts\
If it gives the error "the default argument can only be used 2 times", then the following works some of the time:
setx PATH "%PATH%;C:\Python27\;C:\Python27\Scripts\"
The difference is that we wrapped the second argument in quotes. I believe the quotes are necessary when %PATH%
expands to include spaces.
However, I have encountered some weird problems on Windows 7. On one particular Windows 7 machine, I had this problem:
echo %PATH%
It prints:
C:\Foo\;C:\Bar\;[...lots of stuff...]C:\Baz\
Then I do this:
setx PATH "%PATH%;C:\Quux\"
Then it says "Error: Truncated at 1,024 characters." Now let's check what PATH contains:
echo %PATH%
It prints:
C:\Foo\;C:\Foo\;C:\Bar\;C:\Bar\;[...lots of stuff, now duplicated...]C:\B
...and it is cut off at 1,024 characters. It ran over because of the duplicates. Also interesting: The value of PATH changes despite the fact that setx
raised an error and did not say "Success".
I was able to repeat this strange behavior several times (luckily I had saved the original contents of PATH).
At the moment, the only surefire way I know to append to the PATH is the following:
echo
the PATH.Copy the contents of PATH into a text file and manually add
;C:\Python27\;C:\Python27\Scripts\
to the end of the PATH.Copy the whole thing out of the text file.
setx PATH "<paste the string here>"
That process works every single time on both Windows 7 and Windows 8.
I should really be able to do this in one command. What am I doing wrong?
setx
is dangerous for another reason: if the path contains any environment variables, e.g.,%JAVADIR%\bin
, the reference will be lost, i.e., if JAVADIR changes the path will no longer change with it. If this is a software installer, it may break the end-users machines. Not a good idea. – Longevity"%PATH%;C:\Quux\"
to a different (your own) variable and reference it in theSETX
command line? As inSET "MyPath=%PATH%;C:\Quux\"
SETX PATH "%MyPath%"
. Either way, since this appears to have to do with a known bug in a system tool, Super User may indeed be a better home for this question. – Herriott%
, it interprets it on the spot. – Cresset%PATH%
intosetx
you are losing this information. Plus, of course, the way the OP is doing it the system path setting is getting added to the user path setting, looking at the question again it looks like that's the real cause of the OPs problem. – Longevitysetx
to complete the flush.) ~~~ Bothsetx
andsetx/m
defaults to reg_sz if your input contains 2+%
char. ~~~ "expand" in both hklm and hkcu mean expand to hklm.setx/m qwe %JAVADIR%\bin
andsetx/m javadir asdfg
in either order. Then open a new cmd and doecho %qwe%
: works as expected. – Cresset%JAVADIR%\bin
and the path in HKCU originally contained%JDK%\bin
thenPATH
will be set toC:\Java\bin;C:\jdk\bin
so when you saysetx PATH %PATH%
the value in HKCU changes toC:\Java\bin;C:\jdk\bin
which (a) contains a redundant entry and (b) no longer tracks changes to the value of JDK. – Longevity~
surrounding variables? That doesn't seem to work to solve this problem. – Allenaallenby