0

I am trying to write a batch file running under Windows 8 and Server 2012 R2 where I append a variable to the PATH. The problem is when I use setx, it adds the VALUE of the variable to the path instead of adding the variable itself.

Example: (These of course are not real, only for this example)

My path is C:\Windows
My variable %VAR_1% is set to C:\Windows\system32

If I type in this command:

setx PATH "%PATH%";"%VAR_1%" /M

then what I would expect my PATH to be:

C:\Windows;%VAR_1%;

But instead the PATH becomes:

C:\Windows;C:\Windows\System32;

So the issue is the setx command is adding the value of the variable, instead of the variable itself to the PATH. I've searched on this site (and others), and I've not found the answer to this.

Is there a way to do this with setx?

I've heard there are other tools that can be used for this, but I need to do this with whatever built in tools Windows 8 / Server 2012 R2 has because I would be pushing this batch file to hundreds of computers and I don't want to have to send each user little tool as well.

Mofi
  • 46,139
  • 17
  • 80
  • 143
DOSGUY
  • 1
  • 3
    Double your percent signs (`%%VAR_1%%`). – Ryan Bemrose Nov 16 '16 at 01:03
  • @RyanBemrose, setx.exe allows using `~` to represent `%`, but this is completely the wrong tool to modify `Path`, at least using the current `%Path%` value. It'll make a mess of things -- expanding values that were originally `%` variables and combining the system and user `Path` values. – Eryk Sun Nov 16 '16 at 01:33
  • @DOSGUY Please read the answer on [Why are other folder paths also added to system PATH with SetX and not only the specified folder path?](http://stackoverflow.com/a/25919222/3074564). Never use __local__ `PATH` to extend __system__ `PATH`. The __local__ `PATH` is a combination of __system__ `PATH` and __user__ `PATH` with all environment variables in both `PATH` already expanded. So what you do is corrupting __system__ `PATH`. – Mofi Nov 16 '16 at 06:51
  • @DOSGUY Other Stack Overflow topics which should be read by you completely (question, all answers, all comments) before further processing on your batch coding task for modifying __system__ `PATH`. [How to search and replace a string in environment variable PATH?](http://stackoverflow.com/questions/24648362) [How can I use a .bat file to remove specific tokens from the PATH environment variable?](http://stackoverflow.com/questions/38656375/) [How to set PATH environment variable in batch file only once on Windows?](http://stackoverflow.com/questions/31885906/) – Mofi Nov 16 '16 at 07:07

0 Answers0