sum=how?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
renzlo
Posts: 116
Joined: 03 May 2011 19:06

sum=how?

#1 Post by renzlo » 02 Jun 2011 09:03

i have a text file with these contents:

Code: Select all

total:1
total:2
total:4
total:8


how do write it in batch that will sum up all the total in text file, as for the example above, the answer would be 15.

thanks in advance.

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: sum=how?

#2 Post by nitt » 02 Jun 2011 09:10

renzlo wrote:i have a text file with these contents:

Code: Select all

total:1
total:2
total:4
total:8


how do write it in batch that will sum up all the total in text file, as for the example above, the answer would be 15.

thanks in advance.


If the text file is named "total.txt":

Code: Select all

@echo off
setlocal enableDelayedExpansion
for /f %%a in (total.txt) do (
set text=%%a
set /a total+=!text:~6,10!
)
echo %total%
pause

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: sum=how?

#3 Post by renzlo » 02 Jun 2011 09:21

hi nitt, thanks. Its working. By the way can you give a code that uses "delims=:"?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: sum=how?

#4 Post by dbenham » 02 Jun 2011 10:47

Code: Select all

set total=0
for /f "tokens=2 delims=:" %%n in (file.txt) do set /a total+=%%n


Dave Benham

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: sum=how?

#5 Post by orange_batch » 02 Jun 2011 12:54

I see a pattern 8) fun with bits.

Code: Select all

for /l %%x in (1,1,32) do set /a "num=1<<%%x"&call echo:total:%%num%%

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: sum=how?

#6 Post by Ed Dyreen » 02 Jun 2011 13:02

Code: Select all

set /a "num=1<<1"
echo:total:%num%

Cool orange, but why :?:
Ok, but documentation please
:mrgreen:

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: sum=how?

#7 Post by orange_batch » 02 Jun 2011 13:24

It's just a bitwise operation, look up any DOS reference. They are handy for a number of uses.

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: sum=how?

#8 Post by renzlo » 02 Jun 2011 14:48

thanks everyone for helping.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: sum=how?

#9 Post by Ed Dyreen » 02 Jun 2011 14:53

That's why I couldn't find file 1 :lol:
I completely forgot about those.

Post Reply