adding numbers in a file
Posted: 16 Mar 2011 07:53
hey guys,
i have a script that adds all numbers in a file and appends it to the end of that file, the content of the files looks like this:
==============
113,862
10,430,739
1,111,892
34,005
=============
Is there anyway to modify this script to automatically run for every .txt file in the directory. the file names are random but all alphabetic, and as you can see i have to specify the name of the file each time. i tried *.txt but had no luck with it. the script works; i just need it to run for each file.
any suggestions would be great, thanks in advance.
the script:
================================
@echo off & setlocal EnableDelayedExpansion
set filename=number.txt
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%"
pause
===============================================
i have a script that adds all numbers in a file and appends it to the end of that file, the content of the files looks like this:
==============
113,862
10,430,739
1,111,892
34,005
=============
Is there anyway to modify this script to automatically run for every .txt file in the directory. the file names are random but all alphabetic, and as you can see i have to specify the name of the file each time. i tried *.txt but had no luck with it. the script works; i just need it to run for each file.
any suggestions would be great, thanks in advance.
the script:
================================
@echo off & setlocal EnableDelayedExpansion
set filename=number.txt
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%"
pause
===============================================