Solved Batch File store line variables and sum them up

August 8, 2013 at 23:57:27
Specs: Windows XP
My input file Count.txt contains the following:

] /Count 1
] /Count 2

I am trying to add the numeric characters at the end of each line ad store it to another file or store it to another variable.

I am not able to do it with the following batch file script:

setlocal enabledelayedexpansion
set pagecount=0
set count=0
for /f "tokens=3 delims= " %%i in ('findstr Count Count.txt') do (
set /a count=%%i + %count%
echo !count! > bilang.txt
)
endlocal

The output I am getting is 2.

message edited by Saken


See More: Batch File store line variables and sum them up

Report •

#1
August 9, 2013 at 03:53:57
::====== script starts here ===============
::
:: saken.bat 2013-08-09 17:49:35.57
@echo off & setLocal enableDELAYedeXpansioN

set TOTAL=
for /f "tokens=3 delims= " %%a in (myfile) do (
set/a TOTAL+=%%a
)
echo.!TOTAL!
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

#2
August 9, 2013 at 05:41:13
✔ Best Answer
I like how you don't use delayed expansion for the calculation, but you do for the output.
setLocal enabledelayedexpansion
set count=0
for /f "tokens=3 delims= " %%i in ('findstr Count Count.txt') do (
set /a count += %%i
echo !count! > bilang.txt
) 

EDIT: I'm not sure if you want a straight up count, or a sum of the numbers in the found lines, but I figure between this and Mechanix2Go's, we got you covered.

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

#3
August 9, 2013 at 20:01:04
@echo off & setlocal
set count=0
for /f "tokens=3 delims= " %%i in ('findstr Count Count.txt') do (
set /a count+=%%i
)
echo counted: %count%
>>counted echo %count%
endlocal
::======== end batchscript
kind of a mix-match between the previous two answers, both of which worked...

message edited by nbrane


Report •

Related Solutions

#4
August 11, 2013 at 17:57:21
Thanks guys! All of the suggestions worked

Report •

Ask Question