Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Running total in a DOS loop

Posted on 2011-04-29
3
Medium Priority
?
424 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:Steve4400
3 Comments
 
LVL 11

Expert Comment

by:Paolo Santiangeli
ID: 35493011
0
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 35493275
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
 

Author Closing Comment

by:Steve4400
ID: 35493399
Worked!  Thanks very much.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question