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
===============================================
adding numbers in a file
Moderator: DosItHelp
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: adding numbers in a file
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
-
- Posts: 10
- Joined: 08 Mar 2011 08:54
Re: adding numbers in a file
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
@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