Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
renzlo
- Posts: 116
- Joined: 03 May 2011 19:06
#1
Post
by renzlo » 02 Jun 2011 09:03
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.
-
nitt
- Posts: 218
- Joined: 22 Apr 2011 02:43
#2
Post
by nitt » 02 Jun 2011 09:10
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
-
renzlo
- Posts: 116
- Joined: 03 May 2011 19:06
#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)
#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:
#5
Post
by orange_batch » 02 Jun 2011 12:54
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%%
-
Ed Dyreen
- Expert
- Posts: 1569
- Joined: 16 May 2011 08:21
- Location: Flanders(Belgium)
-
Contact:
#6
Post
by Ed Dyreen » 02 Jun 2011 13:02
Cool orange, but why
Ok, but documentation please
-
orange_batch
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
-
Contact:
#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
#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:
#9
Post
by Ed Dyreen » 02 Jun 2011 14:53
That's why I couldn't find file 1
I completely forgot about those.