computing
  • 3

Solved Merge Multiple CSV Files Using Batch Script

  • 3

I want batch script which would merge/join multiple CSV files from a folder into one.

Each CSV file has 2 columns delimited by comma (,) but number of rows varies. Also each of the CSV file name is unique so when we merge the CSV files I want the file name of the CSV to be the first column for each rows in the file.

So eventually when the script it run it’ll join multiple CSV files under a folder to one. From 2 columns the output file will have 3 columns where the first column would be the file name.

Could anyone please help me with the batch script which can do the above task.

Share

1 Answer

  1. :: ===== script starts here ===============
    :: Rubal February 26, 2013 at 14:59:16
    :: mergecsv.bat 2013-02-26 15:06:35.15
    @echo off & setLocal enableDELAYedeXpansioN

    pushd myfiles
    if exist new.csv del new.csv

    for /f “tokens=* delims= ” %%a in (‘dir/b *.csv’) do (
    set N=%%~Na
    call :sub1 %%a
    )
    goto :eof

    :sub1
    for /f “tokens=* delims= ” %%i in (%1) do (
    echo.!N!,%%i
    )>> new.csv
    goto :eof
    ::====== script ends here =================

    “I think error is from line pushd myfiles . What does this means?”

    You need to change myfiles to the drive and directory where your files ARE.

    =====================
    M2 Golden-Triangle

    • 0