All the solutions below assume all input files are formatted consistently and have the same number of lines.
The technique used at Merge csv file side by side using batch file can be modified to parse and support more than 2 files. I also toggle delayed expansion on and off within the loop to protect any !
that may appear in the data. FOR variables containing !
are corrupted if they are expanded while delayed expansion is enabled:
@echo off
setlocal disableDelayedExpansion
3<"test2.txt" 4<"test3.txt" (
for /f "usebackq tokens=2 delims= " %%A in ("test1.txt") do (
set "A=%%A"
set /p "B=" <&3
set /p "C=" <&4
setlocal enableDelayedExpansion
echo !A! !B:* =! !C:* =!
endlocal
)
) >"result.txt"
The above can be extended to support up to 9 input files using the FOR loop plus handles 0, and 3-9. If you have more than 8 inputs, then you need multiple loops. The first loop can process the first 9 files and write the partial result to a temporary file. Successive loops can read from the temporary file and merge up to 8 additional files.
The above may become cumbersome if your parsing rules become more complex.
My JREPL.BAT hybrid JScript/batch utility can be used to efficiently parse and merge any number of files, and you can modify the regular expressions as needed to parse nearly any csv file format.
@echo off
setlocal
set "merge=jrepl ".*( .*)" "stdin.ReadLine()+$1" /j /f"
jrepl ".* (.*)" "$1" /f test1.txt | %merge% test2.txt | %merge% test3.txt >result.txt
In theory you could use any number of pipes to support all your input files, but if you get too many, it will probably become inefficient. You could use temporary files to stage the merge to preserve efficiency.