Derick Schoonbee
Derick Schoonbee

Reputation: 3021

How can I add totals from a file in a dos batch

I want to add totals from lines in a text file.

My file.txt looks like this:

Totals: 7 passed, 0 failed, 0 skipped
Totals: 10 passed, 0 failed, 0 skipped
Totals: 6 passed, 0 failed, 0 skipped
Totals: 9 passed, 0 failed, 0 skipped
Totals: 4 passed, 0 failed, 1 skipped
Totals: 31 passed, 0 failed, 0 skipped
Totals: 10 passed, 0 failed, 0 skipped
Totals: 4 passed, 0 failed, 0 skipped
Totals: 8 passed, 0 failed, 0 skipped

So when I run sumtotals.bat file.txt then I want something like this:

Passed : XX
Failed : 0
Skipped: X

Upvotes: 2

Views: 59

Answers (1)

wimh
wimh

Reputation: 15232

You can to this:

@echo off

set passed=0
set failed=0
set skipped=0

for /f "tokens=2,4,6 delims= " %%a in (%1) do call :add %%a %%b %%c

echo passed=%passed%
echo failed=%failed%
echo skipped=%skipped%


goto :eof


:add
REM echo %1 %2 %3
set /a passed=%passed%+%1
set /a failed=%failed%+%2
set /a skipped=%skipped%+%3


:eof

result:

C:\temp>sumtotals.bat file.txt
passed=89
failed=0
skipped=1

Upvotes: 4

Related Questions