Rob van der Woude's Scripting Pages

Variable Expansion in FOR Loops

Exercise 1

Create a batch file named VAREXP.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 how the FOR command on the prompt changes with each iteration:
SET VAR
FOR %%A IN (1 2 3) DO SET VAR=%VAR%%%A
SET VAR
@ECHO.
@PAUSE
@GOTO Loop

When you take a look at this batch file, you'll notice that some lines start with @ while others don't.
The ones that don't are the command lines we want to investigate, the other commands will be hidden by the @.

Now start the batch file you just created.
This is what the output should look like:

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

D:\>SET VAR
Environment variable VAR not defined

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

D:\>SET VAR=1

D:\>SET VAR=2

D:\>SET VAR=3

D:\>SET VAR
VAR=3

Press any key to continue . . .

The first 3 (help) lines are static.
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.
But why is it different from the FOR command that was shown in the first 3 (help) lines?
Why %A instead of %%A?
Where is %VAR%?

Before a batch command line is executed, all variables are expanded, replaced by their respective values.
Since VAR didn't have a value yet, %VAR% is replaced by an empty string.

During this first "pass", when %VAR% is replaced by VAR's value, the double percent signs of %%A are replaced by single percent signs, the double percent signs are interpreted as an "escaped" single percent sign.
This also sheds some light on the strange difference in FOR loop syntax on DOS-prompts vs. batch files.

OK, press the anykey ("old" output is marked grey, "new" output is marked black):

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

D:\>SET VAR
Environment variable VAR not defined

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

D:\>SET VAR=1

D:\>SET VAR=2

D:\>SET VAR=3

D:\>SET 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 how the FOR command on the prompt changes with each iteration:

D:\>SET VAR
VAR=3

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

D:\>SET VAR=31

D:\>SET VAR=32

D:\>SET VAR=33

D:\>SET VAR
VAR=33

Press any key to continue . . .

As you can see, in this second iteration, within the FOR loop %VAR% is now permanently replaced by 3, the value it got in the first iteration.

Now try to predict what the output will look like before you 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 how the FOR command on the prompt changes with each iteration:

D:\>SET VAR
Environment variable VAR not defined

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

D:\>SET VAR=1

D:\>SET VAR=2

D:\>SET VAR=3

D:\>SET 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 how the FOR command on the prompt changes with each iteration:

D:\>SET VAR
VAR=3

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

D:\>SET VAR=31

D:\>SET VAR=32

D:\>SET VAR=33

D:\>SET VAR
VAR=33

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 how the FOR command on the prompt changes with each iteration:

D:\>SET VAR
VAR=33

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

D:\>SET VAR=331

D:\>SET VAR=332

D:\>SET VAR=333

D:\>SET VAR
VAR=333

Press any key to continue . . .

I hope you get the idea.

Press the anykey once more and see if you correctly predicted the results.

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


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