3

I have a loop in a batch script and I would like to do some arithmetics with the loop counter. I found out how to evaluate expressions, how to add numbers here: Evaluating expressions in windows batch script

How do I do the same but with variables?

For example:

set x = 100
for /L %%i in (1,1,5) do (
    set Result=0
    set /a Result = %%i+%%x
    echo %Result%
)

As output I would expect

101 102 103 104 105

Thanks!

raoulsson
  • 4,763
  • 10
  • 34
  • 29

2 Answers2

5

You really should move away from Batch files.

@echo off

setlocal enabledelayedexpansion

set x=100
set result=0

for /L %%i in (1,1,5) do (

  set /A result=!x! + %%i

  echo !result!
)

endlocal
Izzy
  • 8,224
  • 2
  • 31
  • 35
1

You are resetting Result to zero at each step. Move that before the loop. Also, try help set at the cmd prompt for more information on all this. Especially look at the section on delayed environment variable expansion.

Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151