
- Batch Script - Home
- Batch Script - Overview
- Batch Script - Environment
- Batch Script - Commands
- Batch Script - Files
- Batch Script - Syntax
- Batch Script - Variables
- Batch Script - Comments
- Batch Script - Strings
- Batch Script - Arrays
- Batch Script - Decision Making
- Batch Script - Operators
- Batch Script - DATE & TIME
- Batch Script - Input / Output
- Batch Script - Return Code
- Batch Script - Functions
- Batch Script - Process
- Batch Script - Aliases
- Batch Script - Devices
- Batch Script - Registry
- Batch Script - Network
- Batch Script - Printing
- Batch Script - Debugging
- Batch Script - Logging
For Statement List Implementations
The "FOR" construct offers looping capabilities for batch files. Following is the common construct of the for statement for working with a list of values.
Syntax
FOR %%variable IN list DO do_something
The classic for statement consists of the following parts −
Variable declaration This step is executed only once for the entire loop and used to declare any variables which will be used within the loop. In Batch Script, the variable declaration is done with the %% at the beginning of the variable name.
List This will be the list of values for which the for statement should be executed.
The do_something code block is what needs to be executed for each iteration for the list of values.
The following diagram shows the diagrammatic explanation of this loop.

Following is an example of how the goto statement can be used.
Example
@echo off FOR %%F IN (1 2 3 4 5) DO echo %%F
The key thing to note about the above program is −
The variable declaration is done with the %% sign at the beginning of the variable name.
The list of values is defined after the IN clause.
The do_something code is defined after the echo command. Thus for each value in the list, the echo command will be executed.
Output
The above program produces the following output.
1 2 3 4 5