Stacking Windows environment variables

1

1

Is it possible to "stack" Windows environment variables such as,

"RUBY_PATH" = "C\ruby\bin"
"PYTHON_PATH" = "C:\python332"
"PROGRAMMING_PATH" = "%RUBY_PATH%;%PYTHON_PATH%"
"Path" = ...;%PROGRAMMING_PATH%'"

I can do it on Linux nodes, but so far, no luck on Windows nodes. Anything I'm not doing right?

Pred

Posted 2013-12-26T16:56:52.610

Reputation: 145

What do you mean stack? You can have multiple environment variables, and each environment variable can have multiple paths... You want to expand environment variables in another variable? – Austin T French – 2013-12-26T16:59:10.570

Well, the example above shows me making two environment variables, then assigning them to one, and then putting that variable into the regular path variable. – Pred – 2013-12-26T17:00:16.327

Answers

2

You can do this easily from the command line:

SET RUBY_PATH=C:\Ruby\Bin
SET PYTHON_PATH=C:\Python332
SET PROGRAMMING_PATH=%RUBY_PATH%;%PYTHON_PATH%
SET PATH=%PATH%;%PROGRAMMING_PATH%

If you are setting these values directly in the registry, you will need to use the REG_EXPAND_SZ data type for the ones that need to be expanded. That is, for the variables that contain other variables.

enter image description here

Patrick Seymour

Posted 2013-12-26T16:56:52.610

Reputation: 7 662

1

I don't see why not, Windows does it out of the box:

enter image description here

I can't say I would personally use it very often, because of the risk of goofing a path up and the not very friendly interface for it. But it looks possible.

Austin T French

Posted 2013-12-26T16:56:52.610

Reputation: 9 766

That works, but I'm doing it multiple times, so Path would contain my PROGRAMMING_PATH, and PROGRAMMING_PATH would then contain the other two paths, RUBY_PATH and PYTHON_PATH. – Pred – 2013-12-26T17:13:22.650

I believe it will work, but more as a byproduct than as a desired parameter of the design of the env variables. If you want to use %variable% as another variable it would be a good idea (algorithm-wise) to have a good recursive function rather than a predictably broken one... Ultimately you might just have to try it once. – Austin T French – 2013-12-26T17:29:12.920

So far, I'm not having much luck. I am able to do as you said in your answer, but more than 2 levels. I can just use a REGEX to clean up my additions like /\%RUBY_PATH\%/''/ – Pred – 2013-12-26T17:39:05.773

0

cmd or batch file:

set

wmic:

wmic ENVIRONMENT get Name, VariableValue, UserName | more

or

wmic ENVIRONMENT get * | more

powershell:

powershell gci env: | more

WSH:

ReadEnv2.vbs | more

ReadEnv2.vbs:

Set WshShell = CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment
WScript.Echo "WINDIR=" & WshEnv.Item("WINDIR")  & vbCrLf &_
                                                  vbCrLf

Set WshShell = CreateObject("WScript.Shell")
WScript.Echo "Environment System:"              & vbCrLf &_ 
"..............................................."
For Each IEnv In WshShell.Environment("System")
    WScript.Echo IEnv
Next

WScript.Echo vbCrLf & "Environment User:"       & vbCrLf &_ 
"..............................................."
For Each IEnv In WshShell.Environment("User")
    WScript.Echo IEnv
Next

WScript.Echo vbCrLf & "Environment Volatile:"   & vbCrLf &_ 
"..............................................."
For Each IEnv In WshShell.Environment("Volatile")
    WScript.Echo IEnv
Next
WScript.Echo vbCrLf & "Environment Process:"    & vbCrLf &_ 
"..............................................."
For Each IEnv In WshShell.Environment("Process")
    WScript.Echo IEnv
Next

STTR

Posted 2013-12-26T16:56:52.610

Reputation: 6 180

0

It seems to depend on how you're setting the environment variables. In the command prompt, you can do this:

set a=C:
set b=%a%\Users
set c=%b%\Thomas
cd %c%

because environment variables get expanded immediately.

But you can't do that:

set c=%b%\Thomas
set b=%a%\Users
set a=C:
cd %c%

On the other hand side it works if you define environment variables via the environment variables dialog and then start a command prompt. It also works in a C# program, when set up in that dialog and then queried by

string c = Environment.GetEnvironmentVariable("c");

Thomas Weller

Posted 2013-12-26T16:56:52.610

Reputation: 4 102