Roy Wood
Roy Wood

Reputation: 79

Query environment variable without evaluating it

I want to query the Path environment variable without evaluating the other variables it contains.

eg.

variable1 = c:\abc
variable2 = c:\def
variable3 = c:\ghi
JAVA_HOME = c:\java
Path = c:\jkl;%variable1%;%variable2%;%variable3%;%JAVA_HOME%

I want to query path and get

c:\jkl;%variable1%;%variable2%;%variable3%;%JAVA_HOME%

NOT

c:\jkl;c:\abc;c:\def;c:\ghi;c:\java

The reason why?

In my groovy script (which is executing shell commands), I want to check if %variable1% (or it's value) has been set in Path. If it hasn't, I want to add it. I should do the following right?

setx path "%path%;%variable1%" /m

Actually no, because this will put into Path c:\jkl;c:\def;c:\ghi;c:\java;%variable1%

Future changes to JAVA_HOME will no longer be reflected in Path. Any other referenced variables in Path that have nothing to do with my program will be changed. Immediately this may not be a problem, but this could impact other programs run by other people if they decide to change something, expecting Path to reflect their changes.

I'm trying to solve this in windows at the moment but will also have to do something similar in linux at some point. I'm currently using Win 7 but this could be run of a variety of OSs.

Upvotes: 0

Views: 85

Answers (1)

Vampire
Vampire

Reputation: 38649

I don't think this is possible. When the variable is set, it is expanded. You would probably have to look into the registry on Windows to get the unexpanded value and will be pretty lost on *nix as you don't know where it gets set. Actually you also don't know in Windows, someone can open a shell, set the variable to some other path and then start your program, then the registry value would be obsolete.

Upvotes: 1

Related Questions