Rob van der Woude's Scripting Pages

Variable Expansion in FOR Loops

Exercise 2

Delayed Variable Expansion

Create a batch file named DELVAREX.BAT containing the following code (use copy and paste):

@SET VAR=
@CD\
@CLS
:Loop
@ECHO.
@ECHO This is the exact FOR loop we're going to run:
@TYPE "%~f0" | FIND "(" | FIND /V "FIND"
@ECHO Note that the FOR command on the prompt does NOT change with each iteration:
SET VAR
FOR %%A IN (1 2 3) DO SET VAR=!VAR!%%A
SET VAR
@ECHO.
@PAUSE
@GOTO Loop

The code is almost identical to that of exercise 1, except for the use of !VAR! instead of %VAR% and the text of the third help line.

Now start the batch file you just created.
This is what the output will look like (if not, for this exercise, start the batch file using CMD /V:OFF /C DELVAREX.BAT):

This is the exact FOR loop we're going to run:
FOR %%A IN (1 2 3) DO SET VAR=!VAR!%%A
Note that the FOR command on the prompt does NOT change with each iteration:

D:\>SET VAR
Environment variable VAR not defined

D:\>FOR %A IN (1 2 3) DO SET VAR=!VAR!%A

D:\>SET VAR=!VAR!1

D:\>SET VAR=!VAR!2

D:\>SET VAR=!VAR!3

D:\>SET VAR
VAR=!VAR!3

Press any key to continue . . .

As in exercise 1, the first 3 (help) lines are static.
And as in exercise 1, the first line "at the prompt" is the SET VAR command, which should display the variable name and its value.
As you can see, the variable doesn't have a value yet (is "not defined").

Next we see the FOR command that is executed.
This time, it doesn't differ much from the FOR command that was shown in the first 3 (help) lines, except for the %A instead of %%A.
That is because !VAR! is not interpreted (expanded) before execution of the FOR loop.

As we can see, at the end of the loop, !VAR! still isn't interpreted.
Press the anykey once more:

This is the exact FOR loop we're going to run:
FOR %%A IN (1 2 3) DO SET VAR=!VAR!%%A
Note that the FOR command on the prompt does NOT change with each iteration:

D:\>SET VAR
Environment variable VAR not defined

D:\>FOR %A IN (1 2 3) DO SET VAR=!VAR!%A

D:\>SET VAR=!VAR!1

D:\>SET VAR=!VAR!2

D:\>SET VAR=!VAR!3

D:\>SET VAR
VAR=!VAR!3

Press any key to continue . . .

This is the exact FOR loop we're going to run:
FOR %%A IN (1 2 3) DO SET VAR=!VAR!%%A
Note that the FOR command on the prompt does NOT change with each iteration:

D:\>SET VAR
VAR=!VAR!3

D:\>FOR %A IN (1 2 3) DO SET VAR=!VAR!%A

D:\>SET VAR=!VAR!1

D:\>SET VAR=!VAR!2

D:\>SET VAR=!VAR!3

D:\>SET VAR
VAR=!VAR!3

Press any key to continue . . .

This is useless.
Press Ctrl+C to end the batch file.

I let you make this "mistake" on purpose, so you'll never forget the second prerequisite: enable delayed variable expansion.

Forgetting to enable it is the most common mistake made with delayed variable expansion.

Start the batch file again, this time from a DOS-prompt, with the following command:

CMD /V:ON /C DELVAREX.BAT

This is what the output should look like now:

This is the exact FOR loop we're going to run:
FOR %%A IN (1 2 3) DO SET VAR=!VAR!%%A
Note that the FOR command on the prompt does NOT change with each iteration:

D:\>SET VAR
Environment variable VAR not defined

D:\>FOR %A IN (1 2 3) DO SET VAR=!VAR!%A

D:\>SET VAR=!VAR!1

D:\>SET VAR=!VAR!2

D:\>SET VAR=!VAR!3

D:\>SET VAR
VAR=123

Press any key to continue . . .

Much better, eh?

Press the anykey:


This is the exact FOR loop we're going to run:
FOR %%A IN (1 2 3) DO SET VAR=!VAR!%%A
Note that the FOR command on the prompt does NOT change with each iteration:

D:\>SET VAR
Environment variable VAR not defined

D:\>FOR %A IN (1 2 3) DO SET VAR=!VAR!%A

D:\>SET VAR=!VAR!1

D:\>SET VAR=!VAR!2

D:\>SET VAR=!VAR!3

D:\>SET VAR
VAR=123

Press any key to continue . . .

This is the exact FOR loop we're going to run:
FOR %%A IN (1 2 3) DO SET VAR=!VAR!%%A
Note that the FOR command on the prompt does NOT change with each iteration:

D:\>SET VAR
VAR=123

D:\>FOR %A IN (1 2 3) DO SET VAR=!VAR!%A

D:\>SET VAR=!VAR!1

D:\>SET VAR=!VAR!2

D:\>SET VAR=!VAR!3

D:\>SET VAR
VAR=123123

Press any key to continue . . .

Exactly what we were looking for!

So in order to use delayed variable expansion in a FOR loop, we need to use !VAR! instead of %VAR%, and enable delayed variable expansion.

Enabling delayed variable expansion can be done using CMD's /V switch, as we used for our exercise, but a better option is to let the batch file itself enable it.
To do so, start the batch file (or subroutine within a batch file) with the command:

SETLOCAL ENABLEDELAYEDEXPANSION

Make sure you close the batch file or subroutine with the command:

ENDLOCAL

Press Ctrl+C to end the exercise and return to the previous page.


page last uploaded: 2016-09-19, 14:57