Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows batch, select ONLY user variables

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.

like image 689
bioShark Avatar asked Nov 13 '12 10:11

bioShark


2 Answers

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 reg utility. There is no ambiguity since they are separate:

  • HKEY_CURRENT_USER\Environment
  • HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

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 cmd window and later modify the environment via system settings dialog, applications started from that cmd won't see the changes.

[UPD] You can get the value of user-specific environment variable from registry using reg utility:

reg query HKCU\Environment /v PATH

Though you'll have to filter its output for the actual value, as it spits out some useless text. Here an example incantation:

for /f "usebackq tokens=2,*" %A in (`reg query HKCU\Environment /v PATH`) do set value=%B

It will store the result in the environment variable value. Remember to double %'s when using it in a batch file.

like image 144
atzz Avatar answered Sep 29 '22 09:09

atzz


If I understand your question, you have 2 %PATH% variables. One system one, and one user one, (presumably you created the latter yourself).

I have tried this and it seems to work for user environment variables

setx /s computername PATH %PATH%;newpathvalue

When I tested this I actually substituted PATH for a new var to make sure it works, but it may be best making a copy of your existing vars before doing this, just in case.

It will tag onto the end of the existing user environment variable PATH with the new value you specify, separating it from the others with a semi-colon ;.

Hope this helps

like image 33
Bali C Avatar answered Sep 29 '22 07:09

Bali C