如何在dos批处理中添加文件中的总计

时间:2011-07-22 11:18:12

标签: windows-xp batch-file

我想从文本文件中的行添加总计。

我的 file.txt 如下所示:

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

所以,当我运行sumtotals.bat file.txt时,我想要这样的东西:

Passed : XX
Failed : 0
Skipped: X

1 个答案:

答案 0 :(得分:4)

你可以这样:

@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

结果:

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