0

I have multiple text files contains integer on each row. Can loop for /f calculate (add) from each row of each file sequentially?

Lets say the text files are like this:

File1.txt
123
213
321

File2.txt
111
222
333

File3.txt
333
222
111

Is it possible iterate over multiple files content and sum the like rows IE:

  • file1 row1 + file2 row1 + file3 row1
  • file1 row2 + file2 row2 + file3 row2
  • file1 row3 + file2 row3 + file3 row3

:: operation for 1st row of each file should be:
Set /A calc=123+111+333
echo !calc!

After googling around, I could not find any solution similar to my problem.

Appreciate if anyone can provide insight on this.

Thanks

T3RR0R
  • 2,747
  • 3
  • 10
  • 25
Zainul
  • 11
  • 1
  • 1
    To get an insight on how this site works, you should please take the [tour], visit the [help] and learn [ask] here! For researching, split the task into smaller sub-items and look them up, because you will probably fail in finding a complete solution here on the web. Once you have tried something on your own but failed, come back here, [edit] your question and provide a [mcve] of your coding attempts… – aschipfl Dec 16 '21 at 18:09
  • In addition to an MRE (Minimal, Reproducible Example), please show what the correct output would be. – lit Dec 16 '21 at 18:56

5 Answers5

0

you can use a single for loop over the output of findstr to build counts from each lines value.

@Echo off & CD /D "%~dp0"
cls

Setlocal

 For /f "tokens=1 Delims==" %%g in ('set Line[ 2^> nul')Do Set "%%g=" 2> nul

 For /f "tokens=2,3 delims=:" %%i in ('%Systemroot%\System32\Findstr.exe /n /r "^[0123456789]*$" "file*.txt"')Do (
  Set /A "Line[%%i]+=%%j+0" 2> nul
 )

 Set Line[
Endlocal
T3RR0R
  • 2,747
  • 3
  • 10
  • 25
0

You can use an array this way:

@echo off
setlocal EnableDelayedExpansion

for %%f in (file*.txt) do (
   set "i=1"
   for /F %%n in (%%f) do set /A "calc[!i!]+=%%n, i+=1"
)

set /A i-=1
for /L %%i in (1,1,%i%) do echo calc[%%i] = !calc[%%i]!
Aacini
  • 65,180
  • 12
  • 72
  • 108
0

This batch-file that is run by cmd uses a PowerShell script. It creates a hash item for each line and accumulates the sum for each.

@powershell.exe -NoLogo -NoProfile -Command ^
    "$h = @{};" ^
    "Get-ChildItem -Filter 'filefile*.txt' |" ^
        "ForEach-Object {" ^
            "$LineNumber = 0;" ^
            "Get-Content -Path $_.FullName |" ^
                "ForEach-Object { $h[$LineNumber++] += [int]$_ }" ^
        "};" ^
    "0..($h.Count-1) | ForEach-Object { $h[$_] }"

This is a lot easier and clearer if written in a .ps1 file.

$h = @{}
Get-ChildItem -Filter 'filefile*.txt' |
    ForEach-Object {
        $LineNumber = 0
        Get-Content -Path $_.FullName |
            ForEach-Object { $h[$LineNumber++] += [int]$_ }
    }
0..($h.Count-1) | ForEach-Object { $h[$_] }

You may see this get downvoted by someone who thinks PowerShell is not part of cmd. PowerShell is just as much a part of cmd as are find.exe, ipconfig.exe, and setx.exe. PowerShell is available on all supported Windows systems.

lit
  • 14,456
  • 10
  • 65
  • 119
0

For a drop single sum files on me approach:-

enter image description here

Summation.bat

@echo off & SETLOCAL ENABLEDELAYEDEXPANSION & Title Summation
for /f "tokens=1* delims=[]" %%A in ('find /n /v "%Title%" "%~1"') do (set "L%%A=%%B" & set "count=%%A")
set calc=0 & for /L %%i in (1,1,!count!) do (set /a calc = !calc! + !L%%i!)
echo/ & echo Line Count = !count! ^& Sum = !calc! & echo/ & pause

Command Line Usage >Summation File1.txt

NOTE as per comment by @T3RROR below

I misread your question as "Provide summation for a file at a time."

HOWEVER have kept it here for others wishing to sum each file.

Good batching, and may the cmd be with you :-)

K J
  • 8,045
  • 3
  • 14
  • 36
0

You can use findstr in a single for loop and set results sequentially per file:

@echo off & setlocal enabledelayedexpansion
for /f "tokens=1*delims=:" %%i in ('findstr /R "[0-9]" file*.txt') do (
   set /a %%~ni+=1 & set /a _Result[!%%~ni!]+=%%j
)
set _Result

Each line per file's lines will be added together, result (based of your current examples: enter image description here

Gerhard
  • 22,678
  • 7
  • 27
  • 43