Jump to content

Recommended Posts

Posted

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.


Posted

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.

Posted

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 been
added.  This support is always disabled by default, but may be
enabled/disabled via the /V command line switch to CMD.EXE.  See CMD /?

Delayed environment variable expansion is useful for getting around
the limitations of the current expansion which happens when a line
of text is read, not when it is executed.  The following example
demonstrates 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 statements
is substituted when the first IF statement is read, since it logically
includes the body of the IF, which is a compound statement.  So the
IF inside the compound statement is really comparing "before" with
"after" which will never be equal.  Similarly, the following example
will 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 the
FOR 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= %i

which just keeps setting LIST to the last file found.

Delayed environment variable expansion allows you to use a different
character (the exclamation mark) to expand environment variables at
execution time.  If delayed variable expansion is enabled, the above
examples 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%

see

http://www.robvanderwoude.com/index.html

for more

jaclaz

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...