How to Generate a Sequence of Numbers in Batch Script
A common requirement in scripting is to perform an action a specific number of times or to iterate through a range of numbers. You might need to create a numbered list of files, process items in a sequence, or simply run a task exactly 10 times. For this, you need a way to generate a sequence of numbers.
This guide will teach you how to use the powerful and efficient FOR /L loop, which is the standard, built-in command in Windows Batch for creating numerical sequences. You will learn its simple syntax and see practical examples of how it can be used for loops and file creation.
The Core Command: FOR /L
The FOR command has several variants, and the /L switch is designed specifically for creating Loops through a sequence of numbers. It is a highly optimized, single-line command that is far more efficient than building a manual counter with SET /A and GOTO.
It is the definitive tool for any task that requires iterating through a numerical range.
The FOR /L Syntax Explained
The syntax for a FOR /L loop is simple and follows a (start, step, end) pattern.
FOR /L %%I IN (start, step, end) DO (command)
%%I: The loop variable. This variable will hold the current number in each iteration of the loop. You can use any letter from A-Z.start: The first number in the sequence.step: The increment value to add in each iteration.end: The last number in the sequence. The loop will continue as long as the current number is less than or equal to this value.
Basic Example: A Simple Counting Loop
This script demonstrates the most common use case: counting from 1 to 5.
@ECHO OFF
ECHO --- Counting from 1 to 5 ---
REM Start=1, Step=1, End=5
FOR /L %%N IN (1,1,5) DO (
ECHO The current number is: %%N
)
ECHO.
ECHO --- Loop finished ---
Output:
--- Counting from 1 to 5 ---
The current number is: 1
The current number is: 2
The current number is: 3
The current number is: 4
The current number is: 5
--- Loop finished ---
How to Count Downwards and Custom Steps
The step value can be negative to create a countdown loop. It can also be a value other than 1 to skip numbers.
Example: How to Count Down
To count down, the start value must be larger than the end value, and the step must be negative.
@ECHO OFF
ECHO --- Countdown from 5 to 1 ---
FOR /L %%I IN (5,-1,1) DO (
ECHO T-minus %%I...
)
ECHO Liftoff!
Output:
--- Countdown from 5 to 1 ---
T-minus 5...
T-minus 4...
T-minus 3...
T-minus 2...
T-minus 1...
Liftoff!
Example: How to Count in Steps of 2
@ECHO OFF
ECHO --- Even numbers from 2 to 10 ---
FOR /L %%E IN (2,2,10) DO (
ECHO %%E
)
Output:
--- Even numbers from 2 to 10 ---
2
4
6
8
10
Common Pitfalls and How to Solve Them
Problem: Using the Loop Variable (%%I)
- In a Batch File: You must use a double percent
%%for the loop variable (e.g.,%%I). - On the Command Line: If you are typing the command directly into
cmd.exe, you must use a single percent%(e.g.,%I).
This is a common point of confusion. The double percent is a special syntax required only inside .bat files.
Problem: Performance with Very Large Ranges
The FOR /L loop is highly optimized and very fast. It can handle millions of iterations with no significant performance issues, as it does not suffer from the same overhead as other batch script loops. For generating numerical sequences, it is the best-performing native tool available.
Practical Example: Creating a Batch of Numbered Files
This script uses a FOR /L loop to create 10 empty, sequentially numbered log files. It also uses a helper script from a previous lesson to pad the numbers with leading zeros for correct alphanumeric sorting.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "OUTPUT_DIR=.\test_logs"
MKDIR "%OUTPUT_DIR%" 2>NUL
ECHO --- Creating 10 Numbered Log Files ---
FOR /L %%i IN (1,1,10) DO (
REM --- Pad the number to 3 digits (e.g., 001, 002) ---
SET "PaddedZeros=000"
SET "TempNum=!PaddedZeros!%%i"
SET "PaddedNum=!TempNum:~-3!"
ECHO Creating log file: log_!PaddedNum!.txt
TYPE NUL > "%OUTPUT_DIR%\log_!PaddedNum!.txt"
)
ECHO.
ECHO --- Done. Listing created files: ---
DIR "%OUTPUT_DIR%" /B
ENDLOCAL
Output:
--- Creating 10 Numbered Log Files ---
Creating log file: log_001.txt
Creating log file: log_002.txt
...
Creating log file: log_010.txt
--- Done. Listing created files: ---
log_001.txt
log_002.txt
log_003.txt
log_004.txt
log_005.txt
log_006.txt
log_007.txt
log_008.txt
log_009.txt
log_010.txt
DelayedExpansion is used here to handle the PaddedNum variable, which is changed inside the loop.
Conclusion
The FOR /L loop is the standard, most efficient, and most readable way to generate a sequence of numbers in a Windows Batch script. It provides a simple and powerful structure for any task that requires numerical iteration.
Key takeaways for using FOR /L:
- Use the
(start, step, end)syntax. - Use a double percent
%%Ifor the variable inside a batch file. - Use a negative step to count downwards.
- The command is highly performant and is the best choice for any numerical looping task.
By mastering the FOR /L loop, you gain a fundamental tool for controlling repetition and creating sequential data in your scripts.