I found plenty of questions like this one on StackExchange but no one work in my specific case.
I would like to easily add literally %FOO% to the user environment variable PATH.
I found the solution below. Unfortunately it doesn't work as expected.
for /f "skip=2 tokens=3*" %a in ('reg query HKCU\Environment /v PATH') do @if [%b]==[] ( @setx PATH "%~a;%FOO%" ) else ( @setx PATH "%~a %~b;%FOO%" )
It doesn't work if:
- The user PATH variable doesn't exist
- The user PATH exists and is empty
- The user PATH is almost 255 char long.
Moreover it doesn't add the %FOO% literally but expands it.
Is there any possibility to easily do it?
Best Answer
It depends on what you're trying to do:
set PATH=%PATH%;%FOO%
.setx PATH "%PATH%;%FOO%"
. Note that this change is not visible in your current command line session; you need to start a new command line.setx /M PATH "%PATH%;%FOO%"
.You can view the path by typing
ECHO %PATH%
in a command line or by checking it in the Windows environment settings.Also, in Windows 7 and 8, the maximum environment variable string size is 32,767 characters. Although this is also valid for the PATH variable, a command in command console has a maximum length of 8191 characters, so you need to take that into consideration when using the PATH variable in command line commands. Anyhow, you have some more head-space above the 256 characters you have right now.