8

Is it possible to merge multiple CSV files [same directory] into one using a batch file? If so, how can I do this?

I think that would apply to any ASCII file?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
D. Caan
  • 1,907
  • 6
  • 22
  • 36
  • do those files have exactly the same column types? – rostok Aug 31 '14 at 20:51
  • Yes, exactly the same. – D. Caan Aug 31 '14 at 20:52
  • Take a look at [Merge / combine / copy the content of text or CSV files into a new file](https://www.ultraedit.com/forums/viewtopic.php?f=52&t=7118) for an UltraEdit scripting solution. I have written there also why I'm using the script and do not use command `copy`. – Mofi Aug 31 '14 at 20:55
  • 1
    Are you *really* using DOS? or do you mean command prompt in Windows?? – aschipfl Oct 07 '15 at 21:16

3 Answers3

13
copy *.csv merged.csv

or

copy file1+file2+… merged.csv

for a more sophisticated script that avoids duplicate headers, see Batch Combine CSV Remove Header

Community
  • 1
  • 1
ths
  • 2,858
  • 1
  • 16
  • 21
  • 1
    if the CSV has column names in first row this will get duplicated in merged.csv. no big deal if the output is meant for manual processing but may get some errors if it is not handled properly – rostok Aug 31 '14 at 20:59
  • @rostok this page (option 2) has the solution to the problem: https://www.itsupportguides.com/office-2010/how-to-merge-multiple-csv-files-into-one-csv-file-using-cmd/ – Rob Sedgwick Jan 01 '17 at 11:30
1

Sometimes you want to know what line came from which file. If you just use

copy *.csv merged.csv

you will get the files pasted one after the other, with no way to know which line came from which file.

A solution involves use of grep.exe utility. You can get it from places such as this one: http://gnuwin32.sourceforge.net/packages/grep.htm

Once you have it (and add its location to your PATH), go to the folder where the csv files are and run this in a command shell:

grep -e ^a*$ *.csv > t.txt

The result will be that all the files are added to t.txt, with the filename appended at the beginning of each line, separated by ":" from the file's text.

Example:

file1.csv

this,line

is,from

file,1

file2.csv

this,line

is,from

file,2

after you run the grep command, t.txt will have:

file1.csv:this,line

file1.csv:is,from

file1.csv:file,1

file2.csv:this,line

file2.csv:is,from

file2.csv:file,2

Change t.txt .t.csv, and open in Excel. The next step is to take the first column and split it to two column, using ":" as a delimiter (Data -> text to columns -> delimited and select "other" and put ":" in the field. )

Careful! add a new column between column A and B, so that when column A is split into 2 columns, it does not overwrite the contents of column B.

Note that if the text in the first csv field contains ":", this will be a bit more difficult as column A will split into more than 2 columns.

Also, the comments about duplicate headers hold here as well.

Michael Stahl
  • 208
  • 3
  • 9
0

For TEXT Files

DEL c:\CombineFiles\CombinedFiles.txt

@echo off>CombinedFiles.txt
for /r  %%a in (.) do copy/b CombinedFiles.txt+"%%a\*.txt" c:\CombineFiles\CombinedFiles.txt

For CSV Files

DEL c:\CombineFiles\CombinedFiles.csv

@echo off>CombinedFiles.csv
for /r  %%a in (.) do copy/b CombinedFiles.csv+"%%a\*.csv" c:\CombineFiles\CombinedFiles.csv

For CSV to TXT files

DEL c:\CombineFiles\CombinedFiles.txt

@echo off>CombinedFiles.csv
for /r  %%a in (.) do copy/b CombinedFiles.csv+"%%a\*.csv" c:\CombineFiles\CombinedFiles.txt
DCCoder
  • 1,587
  • 4
  • 16
  • 29