Specifications

Version 2.0
133
Examples
#
# Sample for loop – listing all .txt files
#
echo -off
for %a in *.txt
echo %a exists
endfor
If in current directory, there are 2 files named file1.txt and file2.txt, the output of the
sample script will be:
Sample1> echo –off
file1.txt exists
file2.txt exists
Theoretically it is legal for 2 nested for commands to use the same alphabet letter as
their index variable, for instance, a.
#
# Sample for loop from 1 to 3 with step 1
#
echo -off
for %a run (1 3)
echo %a
endfor
#
# Sample for loop from 3 down to 1 with step -1
#
echo -off
for %a run (3 1 -1)
echo %a
endfor
#
# Sample for loop – 2 nested for using same index variable
#
echo -off
for %a in value1 value2
for %a in value3 value4
echo %a
endfor
endfor
When processing first for and before seeing the endfor, the index variable %a has
the value “value1”, so in second for, the %a has been already defined and it will be
replaced with the current value of %a. The string after substitution becomes for
value1 in value3 value4, which is not a legal for command. Thus only when the
value of %a is also a single alphabet letter, the script will be executed without error.
If 2 independent for commands use the same index variable, when the second for is
encountered, the first for has already freed the variable so there’ll be no problem in
this case.