Reputation: 311
I would like to create a windows batch file to combine a folder full of CSV's into one CSV file but by column, not at the end.
file1.csv
A, B
1, 2
3, 4
5, 6
...
fileN.csv
C, D
1, 2
3, 4
5, 6
results.csv
A, B, C, D
1, 2, 1, 2
3, 4, 3, 4
5, 6, 5, 6
All of the examples I have seen combine 2 files with a defined name, or a simple copy one after the other. I cannot find how to copy by column!
Upvotes: 2
Views: 1596
Reputation: 67206
I modified the solution I posted at merge-several-csv-file-side-by-side-using-batch-file topic in order to merge several files selected via a wild-card (instead of files given in the parameters). This solution can process a "variable number of files", but just a maximum of 8 files can be merged into another one; if there are more than 8 files, several "merged files" will be created with up to 8 files each in "mergedFiles" folder. If you want to generate one file from more than 8 files, you must repeat the process with the files in "mergedFiles" folder.
@echo off
setlocal EnableDelayedExpansion
rem MergeFiles2.bat: Merge several files horizontally, version 2
rem Antonio Perez Ayala
rem Get the list of files to merge into "file" array
set "num=0"
for %%a in (*.csv) do (
set /A num+=1
set "file[!num!]=%%a"
)
rem Merge files from the "file" array
set /A merge=0, last=0
if not exist mergedFiles\ md mergedFiles
:nextMerge
set /A merge+=1, first=last+1, last+=8
if %last% gtr %num% set last=%num%
rem Assemble the lists of redirections and SET /P commands for this merge
set "file1=!file[%first%]!"
echo/
echo The following files:
echo - %file1%
set /A first+=1
set "redirs="
set "commands="
set handle=2
for /L %%i in (%first%,1,%last%) do (
echo - !file[%%i]!
set /A handle+=1
set "redirs=!redirs! !handle!<"!file[%%i]!" "
set "commands=!commands! & set /P "line=^^!line^^!, " ^<^&!handle!"
)
if defined commands set "commands=!commands:~3!"
echo will be merged into: mergedFiles\file%merge%.csv
rem First file in this merge is read with FOR /F command
rem The rest of files are read via standard handles, starting at # 3
%redirs% (
for /F "usebackq delims=" %%a in ("%file1%") do (
rem Get a line from first file
set "line=%%a"
rem Write-Read lines from all files, excepting the last one
%commands%
rem Write the line from last file
echo !line!
)
) > mergedFiles\file%merge%.csv
if %last% lss %num% goto nextMerge
Upvotes: 2