After some tries, I managed to come up with some basic Environment variable rules when working with command line tools and PATH.
1st. A) Length: No Variable should be longer than 2047. If there are variables longer than 2047, variables after them won't expand, won't show up and Path will become null, PATH=(null).
It's very unlikely to arrive here but as it happens, I was in this situation and this was the main issue, however, this limitation is tricky, because it comes in effect only after editing the Environment variables in the editor (Advanced System Settings / SystemPropertiesAdvanced.exe). It will not have any negative effect if the variables are left unedited after booting up, but, if child explorer are executed for browsing in separate process, the command consoles opened from those explorer windows will be affected.
1st. B) Which Length? It doesn't matter. If a variable is used in Command console it is limited either way to 2047 chars of value, expanded or declared.
Declared:
ex:
x = %variable001%;%variable002%;%variable003%;...;%variable146%;%variable147%
Won't work.
Up to 146, it will because x won't be longer than 2047.
But 147 will kill it. Length of %variable???%; x 147 = 2058.
Expanded:
ex:
x001 = C:\Program Files
x002 = C:\Program Files
x003 = C:\Program Files
...
x120 = C:\Program Files
x121 = C:\Program Files
z = %x001%;%x002%;%x003%;...;%x120%;%x121%;
Will work but will show only up to x120 and part of x121.
Length of C:\Program Files; x 120 = 2040 + C:\Prog of x121
2nd. Place: Expandable variables don't expand recursively. In registry they are of type REG_EXPAND_SZ, in Environment variables editor they are set if the % character is typed in. If there are such variables declared, this needs to be done so that they are initialized before PATH. Normal variables don't have this issue. They can be declared after PATH and their values will still show.
ex:
a = C:\Windows
b (exp) = %SystemRoot%
c (exp) = %a%;%b%;%x%;%y%
x = C:\Windows
y (exp) = %SystemRoot%
z (exp) = %a%;%b%;%x%;%y%
echo %c% will output C:\Windows;C:\Windows;C:\Windows;%y%
a b x y
echo %y% will output C:\Windows
echo %z% will output C:\Windows;C:\Windows;C:\Windows;C:\Windows
a b x y
Variable y was not expanded inside c because it was declared after c, but was expanded in c's clone, z.
So, to keep PATH alive and well, there has to be no lengthy variable before it and it can contain expandable variables as long as they are declared before Path in an alphabetical hierarchy (from a to o). All while keeping a final value length below 2048.
%windir% is its own WinDir environment variable, not part of PATH, so if it's going blank as well you've got larger problems than just the PATH variable. Anything odd in the Event logs? Have you done an
SFC /scannow
yet? – Ƭᴇcʜιᴇ007 – 2014-02-20T16:03:23.320I hate /scannow... messes up sometimes more than it fixes. Did a /verifyonly and it did not find any integrity violations. I did mess around some more with those entries. One of them was called ESET_OPTIONS. Googled it, nothing. It wasn't doing anything besides using up exactly 2079 space chars. Might it be that the whole amount of VARIABLES summed up should not surpass a certain length? Before that var I've put 0SYS as %SystemRoot% and this one shows up. In fact, all vars before ESET_OPTIONS show up. I always thought it's a per variable limitation. – JasonXA – 2014-02-20T19:57:50.510
The environment variables should be automatically trimmed of trailing whitespace before being added. So if it had 2000+ empty characters, it sounds like it was corrupted (garbage that appears as spaces because it's actually unprintable characters), and corrupting anything in memory after the 2079th character (ie: the variables after it). Did completely removing that ESET_OPTIONS variable fix things? – Ƭᴇcʜιᴇ007 – 2014-02-20T20:37:00.817
Not completely. At least now, some expand, others, lower in the list don't. My PATH is now at 68 chars with declared vars and 530 with vars expanded (what gets printed). I've added %windir% last and it didn't expand. 530 isn't near as close to the 2047 limit per variable (http://goo.gl/lfdfBQ), nor does this: "Win XP: The maximum size of the environment block for the process is 32,767 characters. Starting with Windows Vista [...] there is no technical limitation on the size of the environment block." prove correct (http://goo.gl/DaKgWA). My block's apparently limited.
– JasonXA – 2014-02-20T20:55:22.670Gradually, I'm getting to the problem... almost there and it's not a length limitation. – JasonXA – 2014-02-20T21:07:53.713