Limits to environment variable nesting?

trycatch

New member
Local time
11:31 PM
Messages
7
So, is there a limit to how deeply environment variables can be nested in Windows? I do a lot of development work and I'm trying to set up my development environment vars, and a lot of them nest off each other, ie.

FOO=C:\path\to\foo
FOO_BAR=%FOO%\bar
BAR_ETC=%BAR%\etc

I'm having some trouble getting Windows to expand all of them. Eventually when I look at 'set' I get an output like:

FOO=C:\path\to\foo
FOO_BAR=C:\path\to\foo\bar
BAR_ETC=%BAR%\etc


Is there some limit to how deeply it'll expand envvars? Is there a way to coax it into looking more deeply? A registry entry perhaps?

Thanks all!
 

My Computer

OS
Windows 7 / Fedora 12
Have you enabled delayed expansion?

Thanks for your help :)

I'd never heard of that before. Just Google'd around and looked at it, I tried (from cmd) "setlocal enabledelayedexpansion" and echoing a nested library path and it doesn't look like it worked..

Perhaps I'm using it wrong.. everywhere I see it used, it's being used in scripts and such, though I'm not sure how I can make this work for me here. I'm setting these envvars in the actual system properties so they're globally accessible when I'm doing my development work... is there a way to do delayed expansion on these types of envvars?
 

My Computer

OS
Windows 7 / Fedora 12
Try Catch,

Can you post the path names (whether they use variables or not) as you have them set in the Environment Variables dialog in windows. If you need to, you can change the names, for privacy.

Next, can you post a copy of the SET command below that. Again, just change names, like %USERNAME%, for privacy.
 

My Computer

OS
Windows 7
CPU
Quad Core
Memory
8GB
Hard Drives
1TB
I'm not currently at my PC in question but I can give it to you mostly from memory. I've had this issue before and it's usually when I get about five deep that it starts to refuse expanding any further.

It'll be something like:
DEV_HOME=C:\dev
THIRD_PARTY=%DEV_HOME%\thirdparty
OSG=%THIRD_PARTY%\OpenSceneGraph
OSG_ROOT=%OSG%\OpenSceneGraph-2.8.3
OSG_INCLUDE_PATH=%OSG_ROOT%\include
GLEW=%THIRD_PARTY%\GLEW
GLEW_ROOT=%GLEW%\glew-1.5.5
GLEW_INCLUDE_PATH=%GLEW_ROOT%\include


(The reason for the [foo]_ROOT is for keeping multiple versions of dev packages around, I can just toggle [foo]_ROOT to point to a different one...)

When I run set from cmd, I'll get (from memory, I'll post the actual contents later when I get back to my PC, but I'm 99% sure this is right)

DEV_HOME=C:\dev
THIRD_PARTY=C:\dev\thirdparty
OSG=C:\dev\thirdparty\OpenSceneGraph
OSG_ROOT=C:\dev\thirdparty\OpenSceneGraph\OpenSceneGraph-2.8.3
OSG_INCLUDE_PATH=%OSG_ROOT%\include
GLEW=C:\dev\thirdparty\GLEW
GLEW_ROOT=C:\dev\thirdparty\glew-1.5.5
GLEW_INCLUDE_PATH=%GLEW_ROOT%\include

It gets as far as OSG_ROOT and GLEW_ROOT and then OSG_INCLUDE_PATH and GLEW_INCLUDE_PATH refuse to expand. Can't cd to it or anything. I'll double check that this is exactly where it breaks later. Needless to say this is frustrating because that's THE depth layer I need it to get to. D:
 

My Computer

OS
Windows 7 / Fedora 12
I've nested a number of paths, many "variables" deep, but honestly I've really only done it in batch files, and they are tempprary directories. But the concepts are really just the same. However, it would be more helpful, as im sure you know, if I can see the actual data, and not something posted from memory ;)

I'll go home and try some nesting in the env~ var~ dialog and see if I can reproduce the error. I'm personally interested in the outcome myself.
 

My Computer

OS
Windows 7
CPU
Quad Core
Memory
8GB
Hard Drives
1TB
I was in the middle of copying for set and explaining where it went wrong when I realized... everything is expanded now..

Confusion level = high. I haven't rebooted lately, and I know I opened a new cmd after trying the delayed expansion... I need to poke around with this a bit. I don't suppose you were able to force any errors?

EDIT: WAIT, no... GLEW_BIN_PATH fully expands:

GLEW=C:\dev\third-party\GLEW
GLEW_1_5_5=C:\dev\third-party\GLEW\glew-1.5.5
GLEW_BIN_PATH=C:\dev\third-party\GLEW\glew-1.5.5\bin
GLEW_INCLUDE_PATH=C:\dev\third-party\GLEW\glew-1.5.5\include
GLEW_LIB_PATH=C:\dev\third-party\GLEW\glew-1.5.5\lib
GLEW_ROOT=C:\dev\third-party\GLEW\glew-1.5.5
THIRD_PARTY_ROOT=C:\dev\third-party

But in path it doesn't fully decompress:
%THIRD_PARTY_ROOT%\GLEW\glew-1.5.5\bin;

I've got it listed in Path as just GLEW_BIN_PATH%; tacked onto the end of path.

................... and now GLEW isn't expanding and OSG still is....

Confusion level going critical...
 
Last edited:

My Computer

OS
Windows 7 / Fedora 12
AH HA!

Okay I seem to have tracked it down. What was causing one set of dev envvars to expand and the other not had to do with I had some user specific envvars set for OSG and forgot about it, which was causing a few conflicts. Here's what I've tracked down.

It's not nesting, it really is a lack of delayed expansion, but I can't seem to get around it. Here's what's going on...

GLEW/OSG_LIB, _INCLUDE, and _BIN, all depend on GLEW/OSG_ROOT, which is expanded AFTER all of them. So now what I'm getting is _LIB, _INCLUDE, and _BIN are all just having unexpanded variables in them. I've tried enabling delayed expansion but that's not getting me anywhere.. any thoughts (other than renaming _ROOT to something alphabetically first?)
 

My Computer

OS
Windows 7 / Fedora 12
I think I may see some errors in logic here, but give me a few minutes. Also, just an FYI, you would have gotten a lot more responses than one if you posted this to Stack Overflow , a Coders Q&A forum, full of nothing but coders. Or even Server Fault , a Sys Admins forum, full of nothing but sys admins.

Seven Forums is great, but it really has a different focus. IYKWIM
 

My Computer

OS
Windows 7
CPU
Quad Core
Memory
8GB
Hard Drives
1TB
I feel like if I had gone to SO or a coders forum I would have gotten a lot of "WUT IS THIS GO TO A WINDOWS FORUM" responses. lol I can say from experience from living in a few coder IRC channels if you ask something vaguely off topic it can get testy quickly. :/

I restarted and now everything expands EXCEPT I have OSG_BIN_PATH and GLEW_BIN_PATH in my Path variable and they DON'T expand... this is all very odd...
 

My Computer

OS
Windows 7 / Fedora 12
I'm not sure why they are not expanding. Nothing really seems ODD or out of place. I did quickly try building some paths from nested variables, and didn't really have any problems getting them to expand. There are some batch paramaters you can use to expand paths
Code:
[B]%~1[/B]
Expands %1 and removes any surrounding quotation marks ("").
[B]%~f1[/B]
Expands %1 to a fully qualified path name.
[B]%~d1[/B]
Expands %1 to a drive letter. 
[B]%~p1[/B]
Expands %1 to a path.
[B]%~n1[/B]
Expands %1 to a file name. 
[B]%~x1[/B]
Expands %1 to a file extension.
[B]%~s1[/B]
Expanded path contains short names only.
[B]%~a1[/B]
Expands %1 to file attributes.
[B]%~t1[/B]
Expands %1 to date and time of file.
[B]%~z1[/B]
Expands %1 to size of file.
[B]%~$PATH:1[/B]
Searches  the directories listed in the PATH environment variable and expands %1  to the fully qualified name of the first one found. If the environment  variable name is not defined or the file is not found, this modifier  expands to the empty string.
, but I don't think that applies here, in fact I know it doesn't.

Main thing is, its hard to troubleshoot something when you don't post what is actually there, as their might simply be ; or a % missing that is causing the problem.

I'm sure you've been there, when you've debugged something for 5 hours, ready to throw the PC out the window and write yourself off as a programmer, only to later realize that you forgot one letter in a function, or left off a semi colon.

But here is an example of a question just answered on Server Fault

Server Fault





Batch Script With SQLCMD Usage




up vote 0 down vote favorite
Hi All I am Writing a Batch Script Which has to read a set of SQL Files which exists in a Folder then Execute Them Using SQLCMD utiliy.
When I am Trying to execute it does not create any output file. I am not sure where I am wrong and I am not sure how to debug the script. Can someone help me out with script?
@echo off


FOR %F IN (C:\SQLCMD\*.SQL) DO sqlcmd -S LENOVO-C00 -U yam -P yam!@ -i %F -o C:\SEL.txt -p -b


IF NOT [%ERRORLEVEL%] ==[0] goto get_Error


:Success
echo Finished Succesffuly
exit /B 0
goto end

:get_error
echo step Failed
exit /B 40

:end

batch-file
flag
edited 44 mins ago

SvenW
5,345716

asked 53 mins ago

user52128
1








2 Answers

oldest newest votes


up vote 0 down vote
You need two percent signs in your batch file:
FOR %%F IN (C:\SQLCMD*.SQL) DO (
sqlcmd -S LENOVO-C00 -U yam -P yam!@ -i %%F -o C:\SEL.txt -p -b
)
You could also put the "IF NOT" statement just after the sqlcmd, if you wanted to check for an error after each sql command.

link|flag
answered 34 mins ago

jftuga
4955









up vote 0 down vote
Try this line:
FOR %%F IN (C:\SQLCMD\*.SQL) DO sqlcmd -S LENOVO-C00 -U yam -P yam!@ -i %%F -o C:\SEL.txt -p -b
or even better, start debugging with
FOR %%F IN (C:\SQLCMD\*.SQL) DO echo %%F
to see if this loop works, or if there is a problem with the sqlcmd.

link|flag

Now yes, that one was kind of simple, because this guy forgot that he needs %% and not % surronding his variables for a batch file, but the point is, these are pretty seasoned admins who are answering these questions all day. You should give it a shot. Otherwise, please post the output of set (change names for privacy if you need to) and copy and then paste the contents of your env~ var~ as you have them in the windows dialog.
 

My Computer

OS
Windows 7
CPU
Quad Core
Memory
8GB
Hard Drives
1TB
Env var settings:
Code:
GLEW=%THIRD_PARTY_ROOT%\GLEW
GLEW_1_5_5=%GLEW%\glew-1.5.5
GLEW_BIN_PATH=%GLEW_ROOT%\bin
GLEW_INCLUDE_PATH=%GLEW_ROOT%\include
GLEW_LIB_PATH=%GLEW_ROOT%\lib
GLEW_ROOT=%GLEW_1_5_5%

OSG=%THIRD_PARTY_ROOT%\OpenSceneGraph
OSG_2_8_3=%OSG%\OpenSceneGraph-2.8.3
OSG_BIN_PATH=%OSG_ROOT%\bin
OSG_INCLUDE_PATH=%OSG_ROOT%\include
OSG_LIB_PATH=%OSG_ROOT%\lib
OSG_ROOT=%OSG_2_8_3%

THIRD_PARTY_ROOT=C:\dev\third-party


Output of set:
Code:
C:\Users\user>set
ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\user\AppData\Roaming
CommonProgramFiles=C:\Program Files\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
COMPUTERNAME=USER-PC
ComSpec=C:\Windows\system32\cmd.exe
FP_NO_HOST_CHECK=NO
GLEW=C:\dev\third-party\GLEW
GLEW_1_5_5=C:\dev\third-party\GLEW\glew-1.5.5
GLEW_BIN_PATH=C:\dev\third-party\GLEW\glew-1.5.5\bin
GLEW_INCLUDE_PATH=C:\dev\third-party\GLEW\glew-1.5.5\include
GLEW_LIB_PATH=C:\dev\third-party\GLEW\glew-1.5.5\lib
GLEW_ROOT=C:\dev\third-party\GLEW\glew-1.5.5
HOMEDRIVE=C:
HOMEPATH=\Users\user
LOCALAPPDATA=C:\Users\user\AppData\Local
LOGONSERVER=\\USER-PC
NUMBER_OF_PROCESSORS=2
OS=Windows_NT
OSG=C:\dev\third-party\OpenSceneGraph
OSG_2_8_3=C:\dev\third-party\OpenSceneGraph\OpenSceneGraph-2.8.3
OSG_BIN_PATH=C:\dev\third-party\OpenSceneGraph\OpenSceneGraph-2.8.3\bin
OSG_INCLUDE_PATH=C:\dev\third-party\OpenSceneGraph\OpenSceneGraph-2.8.3\include
OSG_LIB_PATH=C:\dev\third-party\OpenSceneGraph\OpenSceneGraph-2.8.3\lib
OSG_ROOT=C:\dev\third-party\OpenSceneGraph\OpenSceneGraph-2.8.3
Path=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program File
s\TortoiseSVN\bin;C:\MinGW\bin;C:\Program Files\MySQL\MySQL Server 5.1\bin;C:\Program Files (x86)\CMake 2.8\bin;%THIRD_PARTY_ROOT%\GLEW\glew-1.5.5\bin;%THIRD_PARTY_ROOT%\OpenSceneG
raph\OpenSceneGraph-2.8.3\bin
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE=AMD64
PROCESSOR_IDENTIFIER=AMD64 Family 16 Model 6 Stepping 2, AuthenticAMD
PROCESSOR_LEVEL=16
PROCESSOR_REVISION=0602
ProgramData=C:\ProgramData
ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)
ProgramW6432=C:\Program Files
PROMPT=$P$G
PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
PUBLIC=C:\Users\Public
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:\Windows
TEMP=C:\Users\user\AppData\Local\Temp
THIRD_PARTY_ROOT=C:\dev\third-party
TMP=C:\Users\user\AppData\Local\Temp
USERDOMAIN=user-PC
USERNAME=user
USERPROFILE=C:\Users\user
VS90COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\
windir=C:\Windows

The only thing I can think of is parsing happening out of order but... I don't know...
 

My Computer

OS
Windows 7 / Fedora 12
Back
Top