Computing Staff
  • 0

Concatenate Multiple Csv Files

  • 0

I have a number of csv files, and I want to concatenate the data in all the csv files in to one. The copy command is working fine for me but I need to remove the header that comes in the output csv file after each csv file is concatenated.

I am using the below code in a batch file:-

@echo off> output.csv
copy /a 1_*.csv Output.csv

Please help

Share

1 Answer

  1. Okay. You have not posted an example of any input file so it’s hit-and-miss as to what will or will not work. Test this, no allowance is made for filenames containing spaces.:

    @echo off
    cls
    setlocal enabledelayedexpansion
    
    if exist output.csv del output.csv
    
    dir /a-d /b Data_*.csv>dirfiles.txt
    
    for /f "tokens=*" %%1 in (dirfiles.txt) do (
        set /p header=<%%1
        echo !header!>output.csv
    )
    
    for /f "tokens=*" %%1 in (dirfiles.txt) do (
            more +1 %%1>>output.csv
       )
    
    del dirfiles.txt
    

    Please come back & tell us if your problem is resolved.

    • 0