I am using the following to create and edit environment variables for Windows 7.

Control Panel\All Control Panel Items\System -> Advanced system settings -> Environment Variables

Under System variables I have the following pertinant variables:


PROG32=C:\Program Files (x86)
REALDWG_SDK_DIR=%PROG32%\Autodesk\RealDWG 2011
Path=%REALDWG_SDK_DIR%;%PROG32%\Haskell\bin

However, the following happens:


C:\>echo %PROG32%
C:\Program Files (x86)

C:\>echo %Path%
%REALDWG_SDK_DIR%;C:\Program Files (x86)\Haskell\bin

Is it possible to have a chain of variables expand?

If I rename Path to something else, I sometimes get the problem, and sometimes I don't.

link|improve this question

75% accept rate
Why don't you use setx to set the variables instead? That way they will be expanded before entering the registry. – paradroid Jan 14 '11 at 9:11
feedback

migrated from stackoverflow.com Jan 14 '11 at 0:52

This question came from our site for professional and enthusiast programmers.

2 Answers

Edit: After experimenting with this a bit more, the actual problem appears to be related to the variable name(s) -- perhaps some kind of bug in the parser that expands nested variables. It appears that the nested variable that you are adding needs to come before the top level variable if they were sorted alphabetically.

As an example, changing REALDWG_SDK_DIR to OEALDWG_SDK_DIR will work, as will BEALDWG_SDK_DIR, but PEALDWG_SDK_DIR will not, nor will ZEALDWG_SDK_DIR. Alternatively, renaming PATH to SATH will work, but RATH will not.

Thus, the solution to your problem is to use something that starts with a letter before P, or else manually type out the first part of the path.


Initial (incorrect) answer:

What you're doing should be working. Check to make sure you don't have a typo in one of the variable names somewhere along the way -- that's the only reason I can think of that would cause it to just print the name without expanding. Also, you will need to open a new command prompt after applying the changes.

link|improve this answer
I second the typo theory. – Randolph West Jan 14 '11 at 2:40
It's not a typo, and I am opening new prompts ever time I change the variables. If I take the PROG32 variable out of REALDWG_SDK_DIR, PATH expands fine. Also, if I rename PATH to something else, it sometimes works fine and sometimes doesn't. – trinithis Jan 14 '11 at 3:10
Interesting... I see what you mean now; I get the same results when using the values you have listed there... aaand, I think I just found the problem -- I'll edit my answer. – Herohtar Jan 14 '11 at 8:35
Thanks. I would accept, but for some reason, I don't own the question. Probably because it was a migrated question and I later made this account here. – trinithis Jan 14 '11 at 16:49
feedback

I wonder if this may related to this defect on Microsoft's Knowledge Base:

http://support.microsoft.com/kb/329308

I've encountered the same problem with environment variables on some XP systems (not only %APPDATA%, like the KB article is about). Using various methods to set the environment variables, including both ones mentioned in the KB article, were of no help... neither was setx (Windows Resource Kit tool).

However, the exact same methods worked on other systems. It is very frustrating - the only solution I've found seems to be using explicit paths rather than environment variable references (which sucks, but works).

Eg,

MYVAR=C:\something
PATH=C:\something\bin

rather than:

MYVAR=C:\something
PATH=%MYVAR%\bin
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.