Reputation: 23
I'm trying to create a .bat file to run on Windows 10. Basically I have multiple .csv files in a folder.
FileA.csv
abc,abc
def,def
FileB.csv
ghi,ghi
jkl,jkl
I want the files to be merged to FileC.csv formatted like this.
abc,abc
def,def
ghi,ghi
jkl,jkl
If I use:
copy /b *.csv FileC.csv
I get this output in FileC.csv:
abc,abc
def,defghi,ghi
jkl,jkl
I need to add a line break between merged files but can't figure out how. Any help would be appreciated.
Upvotes: 1
Views: 7231
Reputation: 1991
@echo off
rem Concatenate input text files, inserting a line break between each file
set inpattern=*.csv
set outfile=all.csv
set count=0
rem Delete any existing output file
del /q %outfile%
rem Count the number of input files
for %%f in (%inpattern%) do (call :count "%%f")
rem Concatenate the input files
for %%f in (%inpattern%) do (call :concat "%%f")
goto :EOF
rem End of main routine
rem Subroutines
:count
rem Increment the input file counter
set /a count+=1
goto :EOF
:concat
rem Append this input file to the output file
type %1 >> %outfile%
rem If this is not the last input file, then append a line break to the output file
if %count% gtr 1 echo.>> %outfile%
rem Decrement the input file counter
set /a count-=1
goto :EOF
About this script:
echo.
command inserts a space (\x20
byte) followed by a Windows-style two-byte line break: a carriage return (Cr, \x0D
) followed by a line feed (Lf, \x0A
).echo.
and >>
is deliberate. If there is a space between echo.
and >>
, then the echo.
"honors" that space, and the output contains two spaces before the line break.The space before the line break annoys me, but I can live with it.
If you cannot live with a space before the line break, or you need a Unix-style line break (or some other type of line break):
linebreak.txt
, that contains only the line break.linebreak.txt
file between the input files, instead of using the echo.
command.@echo off
rem Concatenate input text files, inserting a line break between each file
set inpattern=*.csv
set outfile=all.csv
set linebreak=linebreak.txt
set count=0
rem Delete any existing output file
del /q %outfile%
rem Count the number of input files
for %%f in (%inpattern%) do (call :count "%%f")
rem Concatenate the input files
for %%f in (%inpattern%) do (call :concat "%%f")
goto :EOF
rem End of main routine
rem Subroutines
:count
rem Increment the input file counter
set /a count+=1
goto :EOF
:concat
rem Append this input file to the output file
type %1 >> %outfile%
rem If this is not the last input file, then append a line break to the output file
if %count% gtr 1 type %linebreak% >> %outfile%
rem Decrement the input file counter
set /a count-=1
goto :EOF
Upvotes: 0
Reputation:
In a cmd window this will insert breaks and remove empty lines:
(for /f "delims=" %A in ('Type FileA.csv^&Echo:^&Type FileB.csv') Do @Echo %A)>FileC.csv
In a batch
@Echo off
( for /f "delims=" %%A in (
'Type fileA.csv^&Echo:^&Type FileB.csv'
) Do @Echo:%%A
) > FileC.csv
Upvotes: 0
Reputation: 4934
type A.csv >> C.csv
echo. >> C.csv
type B.csv >> C.csv
>>C.csv
will append the output to an existig file echo.
will print an empty lineUPD
FOR %f IN (*.csv) DO type %f >> ..\newfile.csv & echo. >> ..\newfile.csv
this will merge all .csv
files in new .csv
file in parent folder (if that new file will be same folder, after create first iteration, for also merge that file too)
Upvotes: 6