batch adding multiple numbers in a file

March 15, 2011 at 08:23:36
Specs: Windows XP
hey guys i have a files that has the following content:

=========================================
1,005,941 bytes
11,581,511 bytes
571,856 bytes
167,763 bytes
========================================

i need a script that could add all these numbers together. And append it to the end of the file. so end result being.

=======================================
1,005,941 bytes
11,581,511 bytes
571,856 bytes
167,763 bytes

13,327,111
======================================

thanks in advance


See More: batch adding multiple numbers in a file

Report •


#1
March 15, 2011 at 10:53:40
@echo off & setlocal EnableDelayedExpansion

set filename=HERE YOUR FILE PATHNAME/FILENAME

set sum=0
for /F "tokens=1" %%j in ('type "%filename%"') do (
  set bytes=%%j
  set bytes=!bytes:,=!
  set /A sum+=!bytes!
)
set tot=

:LOOP
set /A res=%sum% %% 1000
set res=00%res%
set res=%res:~-3%
set /A sum=%sum% / 1000
set tot=,%res%%tot%
if %sum% gtr 1000 goto :LOOP

if %sum% gtr 0 (set tot=%sum%%tot%) else (set tot=%tot:~1%)
echo.>> "%filename%"
echo.%tot%>> "%filename%"


Report •

#2
March 15, 2011 at 10:55:27
I think this might work...

@echo off
echo =======================================

for %%A in ("dir c:\ /b") do (
  set Size=%%~zA
  echo %Size%
  Set /a Total=%Total%+%Size%
)
Echo .
Echo Total %Total%
Echo .
echo =======================================

Off the top of my head not tested.


Report •

#3
March 15, 2011 at 11:46:21
Tested Code,,,

@echo off
Set /a Total=0
Set /a Size=0

echo =======================================

for /f "tokens=*" %%I in ('dir /b') do (
  Call :TotalSize %%~zI
  echo %%I %%~zI
)
Echo .
Echo Total %Total%
Echo .
echo =======================================
pause
goto :EOF

:TotalSize
  set Size=%1
  Set /a Total=%Total%+%Size%
goto :EOF


Report •

Related Solutions

#4
March 17, 2011 at 08:08:03
would there be a way to modify the script so that it would run for multiple files in the same directory? i tried *.txt but i need to append it to the same file. let me know.

thanks,


Report •

#5
March 17, 2011 at 09:02:54
I assume you refer to the code I (IVO) suggested in post #1.
If so the following slightly modified script would do the job.

@echo off & setlocal EnableDelayedExpansion

pushd HERE YOUR FOLDER HOLDING THE *.txt FILES

for %%i in (*.txt) do call :SUM "%%i"
popd
goto :EOF

:SUM
set sum=0
for /F "tokens=1" %%j in ('type "%~1"') do (
  set bytes=%%j
  set bytes=!bytes:,=!
  set /A sum+=!bytes!
)
set tot=

:LOOP
set /A res=%sum% %% 1000
set res=00%res%
set res=%res:~-3%
set /A sum=%sum% / 1000
set tot=,%res%%tot%
if %sum% gtr 1000 goto :LOOP

if %sum% gtr 0 (set tot=%sum%%tot%) else (set tot=%tot:~1%)
echo.>> "%~1"
echo.%tot%>> "%~1"
goto :EOF


Report •

#6
March 17, 2011 at 18:33:11
Here's a Ruby for Windows one liner

C:\work> ruby -i.bak -pane 'BEGIN{s=0};s+=$F[0].gsub(",","").to_i;END{print s}' file

C:\work> type file
1,005,941 bytes
11,581,511 bytes
571,856 bytes
167,763 bytes
13327071



Report •

#7
March 18, 2011 at 05:51:04
Thanks IVO that's just that i needed :)

Report •

#8
April 5, 2011 at 06:07:02
bananas

Report •


Ask Question