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=%cwhere
- the
'reg query "HKLM\…\Environment" /v path'string
is thereg querycommand (given above) enclosed in single quotes. a,currentValue, andcare variable names.
You can choose whatever variable names you want,
with the constraints that theaand thecmust be single letters, two letters apart
(e.g., you could usenandp, orxandz).-
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%agets the first token (word) of each (remaining) line,%bgets the second word, and%cgets 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
.BATfile), use%%aand%%c. - Be sure to test this with
echocommands before you do it withsetx.
No comments:
Post a Comment