Skip to main content

How to Use the FOR /L Loop to Iterate Through a Range of Numbers

Looping is a fundamental concept in any programming or scripting language, and the ability to repeat a command a specific number of times is a common requirement. In batch scripting, the FOR /L command provides a simple, powerful, and efficient way to create a numeric loop that iterates through a sequence of numbers.

This guide will teach you the syntax of the FOR /L loop, how to control its start, end, and step values, and show you practical examples of how it's used for repeating actions and processing sequentially numbered files.

The Challenge: Repeating a Command N Times

Without a dedicated loop structure, repeating an action would require you to copy and paste the same line of code multiple times. This is inefficient, hard to read, and impossible to do dynamically (e.g., "run this 100 times"). The FOR /L loop is the definitive solution to this problem.

The Core Command: FOR /L

The FOR command is a multi-purpose tool with several different switches (/F, /D, /R). The /L switch puts the command into a Looping mode that operates on a numeric sequence.

Syntax: FOR /L %%Variable IN (Start, Step, End) DO (command)

  • %%Variable: A single-letter variable (e.g., %%i, %%j, %%k) that will hold the current number on each iteration of the loop.
  • IN (Start, Step, End): The set of three numbers that defines the loop's behavior.
  • DO (command): The command or block of commands to be executed on each iteration.

The Syntax: (Start, Step, End)

The set of numbers in the parentheses controls the loop:

  1. Start: The initial value of the %%Variable. The loop begins with this number.
  2. Step: The increment value. This number is added to the %%Variable after each iteration.
  3. End: The final value. The loop continues as long as the %%Variable is less than or equal to the End value.

Basic Examples of FOR /L

A Simple Count from 1 to 5

This is the most common use case: repeating a command a fixed number of times.

@ECHO OFF
ECHO Starting a simple count from 1 to 5...
FOR /L %%i IN (1, 1, 5) DO (
ECHO The current number is: %%i
)
  • Start: 1
  • Step: 1 (add 1 each time)
  • End: 5

Output:

Starting a simple count 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

Counting Down

To count down, you use a negative step value. In this case, the loop continues as long as the %%Variable is greater than or equal to the End value.

@ECHO OFF
ECHO Starting a countdown from 10 to 1...
FOR /L %%c IN (10, -1, 1) DO (
ECHO ...%%c
)
ECHO Liftoff!
  • Start: 10
  • Step: -1 (subtract 1 each time)
  • End: 1

Output:

Starting a countdown from 10 to 1...
...10
...9
...8
...7
...6
...5
...4
...3
...2
...1
Liftoff!

Counting in Steps

You can use any step value, positive or negative, to create different sequences.

This script counts from 0 to 100 in steps of 10.

@ECHO OFF
ECHO Counting in steps of 10...
FOR /L %%N IN (0, 10, 100) DO (
ECHO Current value: %%N
)
  • Start: 0
  • Step: 10
  • End: 100

Output:

Counting in steps of 10...
Current value: 0
Current value: 10
...
Current value: 90
Current value: 100

Common Pitfalls and How to Solve Them

  • Using a Multi-Letter Variable: The loop variable (%%i) must be a single letter. Using %%counter is not valid syntax.
  • Using Variables in the Range: You can use other variables to define the Start, Step, and End values, which makes the loop dynamic. However, if you are doing this inside another loop, you may need delayed expansion.
    SET "LoopEnd=5"
    FOR /L %%i IN (1, 1, %LoopEnd%) DO ECHO %%i
  • Modifying a Variable Inside the Loop: If you need to use a variable that changes value inside the FOR /L block, you must use Delayed Expansion (SETLOCAL ENABLEDELAYEDEXPANSION and !MyVar!).

Practical Example: Renaming a Sequence of Files

This is a classic use case for a numeric loop. The script finds files named image (1).jpg, image (2).jpg, etc., and renames them to a clean, zero-padded format like photo_001.jpg, photo_002.jpg.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET "count=1"

FOR /L %%i IN (1,1,100) DO (
SET "SourceFile=image (%%i).jpg"

REM Check if the source file exists
IF EXIST "!SourceFile!" (
REM Pad the count with leading zeros
SET "padded=000!count!"
SET "padded=!padded:~-3!"

SET "DestFile=photo_!padded!.jpg"

ECHO Renaming "!SourceFile!" to "!DestFile!"
REN "!SourceFile!" "!DestFile!"

SET /A "count+=1"
)
)

ENDLOCAL

This script intelligently renames up to 100 files, demonstrating how the loop variable %%i can be used to construct dynamic filenames.

Conclusion

The FOR /L loop is the standard and most efficient tool for creating numeric loops in a batch script. Its simple (Start, Step, End) syntax is easy to remember and provides complete control over the iteration.

Key takeaways:

  • The core syntax is FOR /L %%i IN (Start, Step, End) DO (command).
  • The loop variable (%%i) must be a single character.
  • You can count up with a positive step or count down with a negative step.
  • FOR /L is perfect for repeating a command a fixed number of times or for processing items in a known numeric sequence.