Loops
Batch files have a very simple looping mechanism, based loosely on the
for...next loop used in other programming languages. The main difference
is that the batch file for loop doesn??™t increment a variable regularly, but
rather cycles it through a list of values. Its syntax is as follows:
for %%i in ("Abe","Monty","Jasper") do echo %%i
Here, the variable syntax gets even more confusing; the reference to the i
variable when used in conjunction with the for...in...do statement gets
two percent signs in front of the variable name and none after. Note also
that only single-letter variables can be used here.
If you execute this batch file, you??™ll get the following output:
Abe
Monty
Jasper
Note also the use of the quotation marks; although they aren??™t strictly necessary,
they??™re helpful if one or more of the values in the list has a comma in it.
To simulate a more traditional For...Next statement in a batch file, type the
following:
for %%i in (1,2,3,4,5) do echo %%i
Command Prompt Scripting | 563
Scripting and
Automation
Simulating Subroutines
Batch files have no support for named subroutines (as described in ???Make
Building Blocks with Subroutines and Functions,??? earlier in this chapter).
However, you can simulate subroutines by creating several batch files:
one main file and one or more subordinate files (each of which can accept
command-line parameters). You probably wouldn??™t want to use this if performance
is an issue.
Pages:
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784