Page 1 of 1
sum=how?
Posted: 02 Jun 2011 09:03
by renzlo
i have a text file with these contents:
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:
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
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
Cool orange, but why
Ok, but documentation please
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
I completely forgot about those.