Denney Posted August 26, 2004 Posted August 26, 2004 This is aimed at GreenMachine but I would like anyones information on this.I've just come across a problem where I have to use "enabledelayedexpansion" and "!" rather than "&" in my batch files.My question is, would it be OK to use "!" for ALL variables now or just for the ones that require it?The reason I was thinking about using it for everything is that I don't really understand this "Delayed Expansion" thing and it would be easier for me just to use one type of variable and not both.Thanks in advance.
GreenMachine Posted August 26, 2004 Posted August 26, 2004 Sorry, but I'm not sure ... I have used !VAR! when %VAR% would have worked, but my memory fails me as if to whether I have have problems using it when I should not. They way it works, basically, is you need ! when the value for the variable is not known when entering that section of code. A section of code is generally the part inside the parenthesis, as in a FOR loop or IF statement. If the value needs to change inside of this section, you need !. If it will only have one value for the entire section, % can be used. This is because the DOS style interpreter, COMMAND.COM, would interpret yhe whole section before running it. It would assign all the variables beforeexecuting the code. Now you can have the value assigned on the fly, e.g. inside the FOR loop, but need to "expand" the variable after the loop has started (delayed).Hope that made some sense ... I just got up, and I'm on my way out the door.
jaclaz Posted August 26, 2004 Posted August 26, 2004 Yes it is quite a complex thing, maybe this example, taken from Rob van der Woude excellent site helps in clarifying the matter:Finally, support for delayed environment variable expansion has beenadded. This support is always disabled by default, but may beenabled/disabled via the /V command line switch to CMD.EXE. See CMD /?Delayed environment variable expansion is useful for getting aroundthe limitations of the current expansion which happens when a lineof text is read, not when it is executed. The following exampledemonstrates the problem with immediate variable expansion: set VAR=before if "%VAR%" == "before" ( set VAR=after; if "%VAR%" == "after" @echo If you see this, it worked )would never display the message, since the %VAR% in BOTH IF statementsis substituted when the first IF statement is read, since it logicallyincludes the body of the IF, which is a compound statement. So theIF inside the compound statement is really comparing "before" with"after" which will never be equal. Similarly, the following examplewill not work as expected: set LIST= for %i in (*) do set LIST=%LIST% %i echo %LIST%in that it will NOT build up a list of files in the current directory,but instead will just set the LIST variable to the last file found.Again, this is because the %LIST% is expanded just once when theFOR statement is read, and at that time the LIST variable is empty.So the actual FOR loop we are executing is: for %i in (*) do set LIST= %iwhich just keeps setting LIST to the last file found.Delayed environment variable expansion allows you to use a differentcharacter (the exclamation mark) to expand environment variables atexecution time. If delayed variable expansion is enabled, the aboveexamples could be written as follows to work as intended: set VAR=before if "%VAR%" == "before" ( set VAR=after if "!VAR!" == "after" @echo If you see this, it worked ) set LIST= for %i in (*) do set LIST=!LIST! %i echo %LIST%seehttp://www.robvanderwoude.com/index.htmlfor morejaclaz
Denney Posted August 27, 2004 Author Posted August 27, 2004 Thanks for the help guys. It seems that there isn't a problem using "!" all the time.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now