Search code examples
batch-filevariablesfor-loopechoarithmetic-expressions

Perform arithmetic in loop with variables in Batch


I would like to print the following:

0
1
2
3
4

I have tried this:

ECHO OFF
FOR /L %%A in (1,1,5) DO (
    SET /a "B=%%A-1"
    ECHO %B%
)

However, this gives me:

4
4
4
4
4

How can I achieve the desired output while using both A and B in my code?


Solution

  • ECHO OFF
    setlocal
    FOR /L %%A in (1,1,5) DO (
        SET /a "B=%%A-1"
        call ECHO %%B%%
    )
    

    Since you are not using setlocal, B will be set to the value from the previous run. %B% will be replaced by 4 since B was set to 4 by the previous run. the call echo trick uses a parsing quirk to retrieve the current (run-time) value of the variable.

    Here's "the official" way:

    ECHO OFF
    setlocal enabledelayedexpansion
    FOR /L %%A in (1,1,5) DO (
        SET /a "B=%%A-1"
        ECHO !B!
    )
    

    In delayedexpansion mode, !var! retrieves the value of var as it changes at run-time. This is not without its drawbacks, but you'd need to read up on delayedexpansion for a guide on that matter.