This tutorial shows multiple ways to increment a variable in DOS batch programming.

How to increment a variable Windows dos-batch programming?

There are multiple ways we can increment the variable or counter in batch programming.

  • use for loop

for loop is used to run with n number of iterations. for with /L option used to iterate with start=1, stop=1, and end=10 values in the expression The number starts with 1 and is incremented by 1 until the counter is reached 20

 @echo off
 for /L %%n in (1,1,20) do echo %%n

Output:

1
2
3
.
.
20

Another way without for loop

Initializes variable with value 1 Print the value and increment the variable using set /a Print the counter up to 20 times. a

set counter=1
:begin
echo %counter%
set /a counter=counter+1
if %counter% EQU 20 goto end
goto begin

:end