In environment variables I have a PATH variable for both user variables and system variables.
In a batch script, in order for me to append the user PATH variable with a new given path, I need to select the current value. Unfortunately %PATH% returns a combination of the user variable and the system variable.
Of course I only want to add a new custom path value to the user variable. There is no point in enhancing it with the system path as well. That’s why I have 2 variables.
Thanks in advance.
Edit: Found in the documentation the following statement:
The %PATH% variable is set as both a system and user variable, the 2 values are combined to give the PATH for the currently logged in user….
Example:
User variables:
PATH
value: c:\dev
System variables
PATH
value: c:\Program Files
What I want to do, is to add to the user variable the value: c:\tmp, so that in the end PATH will have the value: c:\dev;c:\tmp
But, if open a cmd window:
echo %PATH%
c:\Program Files;c:\dev
so the setx will do the following
setx path "%path%;c:\tmp"
open new cmd
echo %PATH%
c:\Program Files;c:\dev;c:\tmp
And that is wrong, because I only needed c:\dev;c:\tmp
I hope I was more clear this time.
How are you modifying the variables?
There is only one environment variable
PATH, so you can go ahead and change it. These changes are transient (and local to your process and its children).There are two (actually more) persistent places in Registry from which the environment variables are initialized when a process is created. You can modify them using
regutility. There is no ambiguity since they are separate:You may have to re-login for changes in Registry to take effect (I don’t remember whether there’s a programmatic way to notify explorer that these settings have changed). Also note that by default child processes inherit the environment of their parent (unless the parent takes special measures to do otherwise), so e.g. if you launch a
cmdwindow and later modify the environment via system settings dialog, applications started from thatcmdwon’t see the changes.[UPD] You can get the value of user-specific environment variable from registry using
regutility:Though you’ll have to filter its output for the actual value, as it spits out some useless text. Here an example incantation:
It will store the result in the environment variable
value. Remember to double%‘s when using it in a batch file.