Adam Leveille
Adam Leveille

Reputation: 23

Merging text files in .bat with line break between files

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

Answers (3)

Graham Hannington
Graham Hannington

Reputation: 1991

If a space before each line break is acceptable

@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:

  • The 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).
  • The lack of a space in the script between 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.

Otherwise

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):

  1. Create a text file, linebreak.txt, that contains only the line break.
  2. Use the following slightly modified script, which appends the 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

user6811411
user6811411

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

tso
tso

Reputation: 4934

type A.csv >> C.csv
echo. >> C.csv
type B.csv >> C.csv
  1. Type will show the contents of a file
  2. >>C.csv will append the output to an existig file
  3. echo. will print an empty line

UPD

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

Related Questions