Home » Questions » Computers [ Ask a new question ]

Why can't Windows handle an environment variable in Path?

Why can't Windows handle an environment variable in Path?

My colleague and I have identical Dell workstations with Windows XP Professional x64 edition installed.

Asked by: Guest | Views: 353
Total answers/comments: 4
Guest [Entry]

"There is a definite problem with expanding environment variables within the PATH variable when the variable expands to a path that contains spaces.

We created our own system level variables like ""OUR_ROOT=c:\MyRoot"" and then used it in system PATH like ""PATH=;%OUR_ROOT%\bin;"" and that gets expanded correctly to ""PATH=;c:\MyRoot\bin;"". So far no problem.

But, on Windows 7 (32-bit), I had a product install itself and create system environment variables like this:

STUDIO_BIN=C:\program files\Company Name\Product Name 10.4\bin

and it added it to the system PATH variable:

PATH=<other path elements>;%STUDIO_BIN%;<more path elements>

But the PATH values shown in CMD contained ""%STUDIO_BIN%;"" and not the expanded path. The value in My Computer > Properties > Advanced > Env.Vars remained unexpanded as well. This meant I couldn't run programs that required a DLL in that directory.

By just changing STUDIO_BIN (via My Computer>Properties>Advanced ...>Env Vars) to a name without embedded spaces :

STUDIO_BIN=C:\ProductName\bin

and then restarting the CMD window, the PATH is now:

PATH=<other path elements>;C:\ProductName\bin;<more path elements>

Another solution is to sufficiently edit the system variable you are using in the PATH using the My Computer > Properties > Advanced... > Environment Variables dialog.
I tried adding a character and removing it to make a 'change' and then OK'd out, started a new CMD prompt and PATH was NOT correctly expanded.
I then tried deleting part of the path so it was

STUDIO_BIN=C:\Program Files\Company Name

(omitting ""Product Name 10.4"") and lo, and behold, the next CMD prompt showed PATH with STUDIO_BIN properly expanded!

Strangely enough, if I went back in and added the ""Product Name 10.4"" to STUDIO_BIN (including all the spaces that were originally there before I started mucking with it) and PATH was STILL correctly expanded.

Evidently with enough change to its contents, the PATH variable undergoes some extra processing in the Environment Variables dialog that allows it to work. Processing that's not done when the variable was added by the product's installer (which probably just modified PATH in the registry directly).

I'm almost positive this was a problem with XP as well. It just resurfaced for me in Windows 7 as I was putting together a new development machine. Apparently it has not been fixed by Microsoft.

Apparently even MS defined variables like %ProgramFiles% won't expand correctly in the PATH.

This page provides a possible answer if you're setting PATH via the command-line or batch file. (Enclose the whole command after SET in quotation marks.) I don't know what installer the product I installed used to set the environment variables, but it evidently went around whatever processing is needed to properly expand the paths with spaces.

So - to summarize, you can either:

change the paths (and move all associated files) to paths without spaces, or
edit the variables that are failing to expand in the Environment Variables dialog (changing them enough to get them to process correctly - I'm not positive how much is enough)."
Guest [Entry]

"Add the environment variables whilst logged onto the /console session using MSTSC.

Reboot the machine and you will find your environment variables will have persisted.

There appears to be a quirk in the O/S depending on how you were connected to the machine when you attempted to change the environment variable."
Guest [Entry]

"You have to consider the order in which variables are set upon login. If you try to use a variable before it is set, it will come out as the empty string.

The effective PATH is the concatenation of the user's PATH variable followed by the global PATH variable.

User variables are set before global variables, so you can't use global variables in you user PATH variable. Additionally, variables are set in alphabetical order, so you can't use variables that sort before PATH.

(This applies to Windows 7 at least. I haven't tested this on newer versions.)"
Guest [Entry]

"It might be related to the ""delayed environment variable expansion"" feature (or lack thereof), or perhaps you can take advantage of this feature to always have a correct solution.

from a cmd prompt

set /?

and read the section describing ""delayed environment variable expansion"", which includes a small example to test

set VAR=before
if ""%VAR%"" == ""before"" (
set VAR=after
if ""%VAR%"" == ""after"" @echo If you see this, it worked
)

If you don't get the echo line, then that might explain it...

If, however, you start your cmd.exe with /V option, then you can use ""!"" instead of ""%"", which changes the behaivior

set VAR=before
if ""%VAR%"" == ""before"" (
set VAR=after
if ""!VAR!"" == ""after"" @echo If you see this, it worked
)

For me (running on XP), the 1st script did not work, but the second version did (with cmd.exe /V)"