Running total in a DOS loop

I am having trouble creating a running total in a DOS loop.
this is what I have inside the loop:
     set total=%%H
     set /a tot=%total%+%%H
     echo. %tot%
total is initalized to zero
my results look like:
0+2346
0+4524
0+3987 ect.
Any help would be greatly appreciated.  Thanks.
Steve4400Asked:
Who is Participating?
 
oBdAConnect With a Mentor Commented:
You're a victim of variable expansion in a for loop; the interpreter reads the complete loop, expands all normal variables, and uses these values (enter "help set" for details).
You need to enable delayed expansion; in addition, there is no need to use percentage signs in a "set /a" command.
Compare the output of the following commands:
for /l %a in (1, 1, 10) do (set /a i = %i% + 1)

Open in new window

and
for /l %a in (1, 1, 10) do (set /a j = j + 1)

Open in new window

So try something like this (note the "!" being used to expand the "total" variable in the loop!):
@echo off
setlocal enabledelayedexpansion
set total=0
for /l %%H in (1, 1, 10) do (
     set /a total = total + %%H
     echo !total!
)

Open in new window

0
 
Paolo SantiangeliConsulente InformaticoCommented:
0
 
Steve4400Author Commented:
Worked!  Thanks very much.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.