Imagine the following environment variables:
System PATH = C:\Windows
Bob's User PATH = C:\Users\Bob
In a command prompt the PATH
command returns C:\Windows;C:\Users\Bob
After running setx /m PATH "C:\Node;%PATH%"
System PATH = C:\Node;C:\Windows;C:\Users\Bob
In a new command prompt the PATH
command returns C:\Node;C:\Windows;C:\Users\Bob;C:\Users\Bob
Another user, Alice, logs in.
Alice's User PATH = C:\Users\Alice
On a command prompt the PATH
command returns C:\Node;C:\Windows;C:\Users\Bob;C:\Users\Alice
Bob has a duplicate path in his PATH
variable, and Alice has Bob's paths in her PATH
variable.
Is there a way to append to the System PATH
without polluting it with the current user's PATH
?
Answer
In Windows 7, you can look up the System Path with
reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v path
If that still works on Windows 8, then use that to build the new value.
You'll have to figure out how to parse the output of reg query
.
Here's something that might work:
for /f "tokens=1,2*" %a in ('reg query "HKLM\…\Environment" /v path') do set currentValue=%c
where
- the
'reg query "HKLM\…\Environment" /v path'
string
is thereg query
command (given above) enclosed in single quotes. a
,currentValue
, andc
are variable names.
You can choose whatever variable names you want,
with the constraints that thea
and thec
must be single letters, two letters apart
(e.g., you could usen
andp
, orx
andz
).-
for /f "options" %variable in ('command1') do command2
runscommand1
, parses the output, assigns value(s) to the%variable
(s) (%a
, above; but see also below) and executescommand2
. tokens=1,2*
means that%a
gets the first token (word) of each (remaining) line,%b
gets the second word, and%c
gets the rest of the line.- The first word is
path
(the value name). - The second word is
REG_EXPAND_SZ
(the value type). - The rest of the line is the value.
(You could just use
tokens=2*
and thencurrentValue=%b
.)- The first word is
So, after executing the above, you should be able to do
setx PATH "C:\Node;%currentValue%" /m
- If you do this in a script (a
.BAT
file), use%%a
and%%c
. - Be sure to test this with
echo
commands before you do it withsetx
.
No comments:
Post a Comment