How to use DOS batch commands for “For” statements.
The FOR command can be used to execute a specified command multiple times within a batch script, or to manipulate a group of files. Here are some examples of how to use the FOR command:
- Traverse the files in the directory.
FOR %i IN (C:\path\to\files\*.txt) DO echo %i
This command will list all .txt files in the specified directory and output their names.
- Iterate through a sequence of numbers.
FOR /L %i IN (1,1,10) DO echo %i
This command will output a sequence of numbers from 1 to 10.
- Iterate through a string sequence.
FOR %i IN (apple orange banana) DO echo %i
This command will output three strings: apple, orange, and banana.
- Iterate through a list.
FOR %i IN (1,2,3,4,5) DO echo %i
This command will output 1, 2, 3, 4, and 5.
Please note that when using the FOR command in a batch script, you should use single percentage signs to reference variables, and if using the FOR command in the command line, you should use double percentage signs.