Ahamed Moosa
Ahamed Moosa

Reputation: 1445

Merging multiple csv files in cmd

I have multiple csv files of large size in a folder, say

file1.csv
file2.csv
file3.csv
combine.csv 

And if I have to combine (append) all of them using cmd, I know that I have to use

copy *.csv targetfile.csv

My question is what if I have to combine the csv files which has the name "file" in it which are file1.csv, file2.csv, file3.csv in this case.

Upvotes: 2

Views: 28397

Answers (2)

Ved Prakash Shukla
Ved Prakash Shukla

Reputation: 41

For this In Mac/Linux You can use Cat command

cat *.csv >targetData.csv

And for Windows users have to do same with Type command

type *.csv >targetData.csv

Upvotes: 2

user7818749
user7818749

Reputation:

@aschipfl already answered this in a comment, but I am answering this with some explanations:

copy file*.csv targetfile.csv

The above purely runs a single copy command for all file*.csv files and once off opens targetfile.csv and appends each file's content to it.

Additionally, if you have 100s of different filenames all together with csv extensions, you can also do this and ensure you exclude targetfile.csv

for %i in (*.csv) do if not "%i" == "MyTextOutput.csv" type %i >> targetfile.csv

The exact same line can be used in a batch file by simply adding an additional % to the meta variables.

@echo off
for %%i in (*.csv) do if /i not "%%i" == "targetfile.csv" type %%i >> targetfile.csv

Upvotes: 13

Related Questions