Page 1 of 1

sum=how?

Posted: 02 Jun 2011 09:03
by renzlo
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.

Re: sum=how?

Posted: 02 Jun 2011 09:10
by nitt
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

Re: sum=how?

Posted: 02 Jun 2011 09:21
by renzlo
hi nitt, thanks. Its working. By the way can you give a code that uses "delims=:"?

Re: sum=how?

Posted: 02 Jun 2011 10:47
by dbenham

Code: Select all

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


Dave Benham

Re: sum=how?

Posted: 02 Jun 2011 12:54
by orange_batch
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%%

Re: sum=how?

Posted: 02 Jun 2011 13:02
by Ed Dyreen

Code: Select all

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

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

Re: sum=how?

Posted: 02 Jun 2011 13:24
by orange_batch
It's just a bitwise operation, look up any DOS reference. They are handy for a number of uses.

Re: sum=how?

Posted: 02 Jun 2011 14:48
by renzlo
thanks everyone for helping.

Re: sum=how?

Posted: 02 Jun 2011 14:53
by Ed Dyreen
That's why I couldn't find file 1 :lol:
I completely forgot about those.