How to merge (combine) multiple CSV files into one CSV file using CMD

Here’s a useful tip if you ever need to combine multiple CSV files into one CSV file.

For example, if you have multiple CSV files but need to run a single report (such as a crystal report) based on the data.

Option 1 – Combine CSV files without a header row

The following single command line will combine all CSV files in the folder as a single file titled ‘combined.csv’

copy *.csv combined.csv

csv-combine-1

If you want to run this from a cmd file, copy the following contents into a text file and save as ‘run.cmd’.

This command will automatically run from the folder the file is saved in, that is – if you save it to C:\TEMP it will look for CSV files in C:\TEMP and save the new file to C:\TEMP

@echo off
copy %~dp0*.csv %~dp0combined.csv

Option 2 – Combine CSV files with header row

So what if your source files have a header row? The following command will take the header from the first file, then exclude it from the rest. Copy the following contents into a text file and save as ‘run.cmd’.

@ECHO OFF

REM Set working directory
pushd %~dp0

REM Delete existing combined file
del combined.csv

setlocal ENABLEDELAYEDEXPANSION
set total=0
set count=0

REM Set total
for %%i in (*.csv) DO set /a total+=1

for %%i in (*.csv) DO (
cls
echo:Combining CSV files [!count!/%total%]
if !count!==0 (
for /f "delims=" %%j in ('type "%%i"') do echo %%j >> combined.csv
) else if %%i NEQ combined.csv (
for /f "skip=1 delims=" %%j in ('type "%%i"') do echo %%j >> combined.csv
)
set /a count+=1
)

ECHO Finished
PAUSE >NUL

Having troubles?

You can download an example of this script below: www.itsupportguides.com/downloads/csvmerge.zip

Note: this process will only work for CSV files – not XLS (or similar) files.

Article Downloads

TIP: You may need to right-click and select 'save link as'.

Tagged in

32 comments on “How to merge (combine) multiple CSV files into one CSV file using CMD

  1. Buenas tardes. Buen programa, pero el contador debe comenzar en 1, no en CERO. Me ahorraste un par de horas. Abajo la corrección.

    @ECHO OFF

    REM Set working directory
    pushd %~dp0

    REM Delete existing combined file
    del combined.csv

    setlocal ENABLEDELAYEDEXPANSION
    set total=0
    set count=1

    REM Set total
    for %%i in (*.csv) DO set /a total+=1

    for %%i in (*.csv) DO (
    cls
    echo:Combining CSV files [!count!/%total%]
    if !count!==1 (
    for /f “delims=” %%j in (‘type “%%i”‘) do echo %%j >> combined.csv
    ) else if %%i NEQ combined.csv (
    for /f “skip=1 delims=” %%j in (‘type “%%i”‘) do echo %%j >> combined.csv
    )
    set /a count+=1
    )

    ECHO Finished
    PAUSE >NUL

  2. Hi,

    this script is just great, and it works for what I am needing. However, I have a question and hope you can give me a hand. I need to merge only the 3 or 4 last files within that folder, so, is there any way to select the files that need to be combined?

    Thanks a lot,

  3. This still works. And thanks for the lesson.

    Did want to say I had 18mb between 28 files. I’m not using a high test pc right now, and the operation happened very quickly. 7 seconds or so. If that.

    Thanks again!

Leave a Comment

Your email address will not be published. Required fields are marked *