windows batch script get environment variable changes

0

I have a script that does something like

setx %PATH% %PATH%;%ANOTHER_VAR%

echo %PATH%

When I echo the path, it does not include %ANOTHER_VAR%. is there a way to ensure the new variables are reflected the second time I echo %PATH%

EDIT: I'm writing a puppet script that calls 2 separate exec commands. The first calls setx on a variable. Since the second one is accessed in a new exec command, I would expect it to have access to the newly set variable, but it does not.

I have also tried just creating a variable in script1 and accessing that from script2, but it's not accessible (as expected).

Jeff Storey

Posted 2012-03-01T02:48:57.783

Reputation: 375

Answers

1

As I understand it, setx sets the variable via the registry (see this article). It requires the start of a new command shell to see the results.

If you want to change the path setting under the current command session use set path=%path%;%another_variable%.

In response to the comment, this is from setx /?:

NOTE: 1) SETX writes variables to the master environment in the registry.

2) On a local system, variables created or modified by this tool will be available in future command windows but not in the current CMD.exe command window.

If you want to change the variable in the current command session, use plain SET.

Below is the output from my system:

C:\Windows\system32>set path2=%cd%

C:\Windows\system32>set path2
path2=C:\Windows\system32

C:\Windows\system32>cd drivers

C:\Windows\System32\drivers>set path2=%path2%;%cd%

C:\Windows\System32\drivers>set path2
path2=C:\Windows\system32;C:\Windows\System32\drivers

Scott McKinney

Posted 2012-03-01T02:48:57.783

Reputation: 884

Hmmm..I must be doing something else wrong. I have one script that is calling the other 2 and the 2nd script does not pick up the changes via setx. I might just be expecting something that shouldn't happen. – Jeff Storey – 2012-03-01T03:03:33.803