Page 1 of 1

adding numbers in a file

Posted: 16 Mar 2011 07:53
by cactusman252
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
===============================================

Re: adding numbers in a file

Posted: 16 Mar 2011 20:38
by ghostmachine4
Provided you are sure you don't have decimal numbers to add, otherwise, use a better programming tool, such as vbscript (or others ) because batch does not do decimals/floats.

Code: Select all

Set objFS = CreateObject( "Scripting.FileSystemObject" )
strFolder = WScript.Arguments(0)
Set objFolder = objFS.GetFolder(strFolder)
total=0
strOutput = "temp"
Set objOutFile = objFS.CreateTextFile(strOutput,True)
For Each strFile In objFolder.Files
   strFileName = strFile.Name
   If objFS.GetExtensionName(strFile) = "txt" Then
      If strFileName = "file.txt" Then 'comment this out for processing all txt files
            Set objFile = objFS.OpenTextFile(strFile)
            Do Until objFile.AtEndOfStream
               strLine=objFile.ReadLine   
               s = Split(strLine   ,",")
               For i=LBound(s) To UBound(s)
                  total = total + s(i)
               Next         
               objOutFile.WriteLine(strLine)
            Loop
            objFile.Close
            objOutFile.WriteLine(total)
            objOutFile.Close            
            objFS.CopyFile  strOutput, strFile.Name
            objFS.DeleteFile strOutput
            WScript.Echo "Total: " & total
      End If ' comment this out for processing all txt files
   End If    
Next



Code: Select all

C:\test>cscript //nologo test.vbs c:\test

Re: adding numbers in a file

Posted: 18 Mar 2011 06:08
by cactusman252
Fixed it. I had this in mind.

@echo off & setlocal EnableDelayedExpansion

pushd Y:\test 3 *.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