Skip to main content

How to Increment a Number in a Loop in Batch Script

Incrementing a counter is one of the most common and fundamental operations inside a loop. You might need to count the number of files processed, limit a loop to a specific number of iterations, or simply number your output lines. While the math itself is simple using the SET /A command, doing it correctly inside a FOR loop requires understanding a critical concept in batch scripting: Delayed Expansion.

This guide will first show you the classic way this fails for beginners, explain why it fails, and then teach you the correct and robust method using delayed expansion. Mastering this technique is the key to writing almost any advanced FOR loop.

The Core Command for Math: SET /A

The SET /A command is the built-in tool for performing Arithmetic in batch scripts. To increment a number, you have a few easy syntax options.

Assuming you have a variable count with a value of 0:

  • SET /A "count = count + 1": The most explicit way.
  • SET /A "count+=1": A common shorthand for incrementing.
  • SET /A "count++": A shorthand popular in other languages (works for pre- or post-increment, but is less readable in batch).

All three of these commands will correctly add 1 to the count variable.

The Critical Pitfall: Why Standard Expansion (%Var%) Fails in a Loop

This is one of the most common and frustrating problems for new batch scripters. Let's try to use our SET /A command inside a standard FOR loop.

Example of script with the flaw:

@ECHO OFF
SET count=0

ECHO Starting loop...
FOR /L %%i IN (1,1,3) DO (
SET /A "count+=1"
ECHO The current count is: %count%
)
ECHO The final count is: %count%

You would expect the output to be 1, 2, 3. Instead, you get this WRONG Output:

Starting loop...
The current count is: 0
The current count is: 0
The current count is: 0
The final count is: 3

Why this happens: The command processor (cmd.exe) reads the entire FOR loop block (from ( to )) at once. During this initial parsing phase, it replaces all standard variables (%count%) with their value at that moment. Before the loop started, %count% was 0. So, the ECHO command inside the loop was effectively hardcoded to ECHO The current count is: 0 for all three iterations. The SET /A command is working correctly (as the final result of 3 shows), but you are never able to see its updated value inside the loop.

Solution: Enabling and Using Delayed Expansion (!Var!)

To fix this, you need to tell the command processor to wait and get the current value of the variable at the moment of execution. This is called Delayed Expansion.

There are two steps:

  1. Enable it: Add SETLOCAL ENABLEDELAYEDEXPANSION at the start of your script.
  2. Use it: Access the variable using exclamation marks (!) instead of percent signs (%).

The Corrected Script:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET count=0

ECHO Starting loop...
FOR /L %%i IN (1,1,3) DO (
SET /A "count+=1"
ECHO The current count is: !count!
)
ECHO The final count is: !count!

and the CORRECT Output

Starting loop...
The current count is: 1
The current count is: 2
The current count is: 3
The final count is: 3

This now works exactly as expected.

How Delayed Expansion Works

  • Static Expansion (%Var%): Replaced with the variable's value when the command block is first parsed. It sees the "before" value.
  • Dynamic/Delayed Expansion (!Var!): Replaced with the variable's value when the command is executed. It sees the "live" or "current" value.

This is why delayed expansion is essential for any loop where a variable's value is both changed and read within the same block.

Practical Example: Numbering Files While Processing Them

This is a very common use case. The script iterates through all .txt files in a folder and prints them out with a sequential number.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET "folder=C:\MyLogs"
SET "file_counter=0"

ECHO --- Processing text files in %folder% ---
ECHO.

FOR %%F IN ("%folder%\*.txt") DO (
REM Increment the counter for each file found
SET /A "file_counter+=1"

REM Display the current count and the filename
ECHO Processing file !file_counter!: "%%F"

REM (You could put other commands here, like MOVE, COPY, etc.)
)

ECHO.
ECHO --- Found and processed !file_counter! files. ---

ENDLOCAL

Here, !file_counter! correctly updates on each iteration, providing an accurate, numbered list of the files as they are processed.

Conclusion

Incrementing a number in a batch script is easy, but doing so inside a loop requires a specific and crucial technique.

  • Use the SET /A command to perform the mathematical increment (e.g., SET /A "count+=1").
  • You must enable delayed expansion at the start of your script with SETLOCAL ENABLEDELAYEDEXPANSION.
  • You must use exclamation marks (!MyVar!) to access the changing value of the variable inside the loop.

Mastering delayed expansion is the single most important step in moving from simple, linear batch scripts to writing complex and powerful loops.