0

I have a zip folder, under that I have many sub folders and each sub folder has some csv files. Now i want to copy (merge) all the csv files from different sub folders into one csv file... by using only batch scripting

Prasad
  • 21
  • 5
  • 2
    Possible duplicate of [Merge multiple csv files using batch file](http://stackoverflow.com/questions/25596171/merge-multiple-csv-files-using-batch-file) – wOxxOm Oct 07 '15 at 15:21
  • 1
    @Prasad, use Google or other search site before asking. I see several solutions for this query: [windows merge multiple csv batch file](https://www.google.com/#q=windows+merge+multiple+csv+batch+file). – wOxxOm Oct 07 '15 at 15:22

1 Answers1

1

The code below searches through subdirectories with DIR /S looking for .csv files. Upon finding one, it appends the contents to the file named in the SUMMARY_FILE variable.

SETLOCAL ENABLEDELAYEDEXPANSION

SET SUMMARY_FILE=C:\Users\bone\sumfile.csv
IF EXIST "%SUMMARY_FILE%" (DEL "%SUMMARY_FILE%")
CD C:\root\of\all\csv\files

SET /A LINE_COUNT=1

FOR /F "usebackq tokens=*" %%f IN (`DIR /S /B *.csv`) DO (
    FOR /F "usebackq tokens=*" %%s IN (`TYPE "%%~f"`) DO (
        ECHO !LINE_COUNT!,%%s >>"%SUMMARY_FILE%"
        SET /A LINE_COUNT=!LINE_COUNT! + 1
    )
)
EXIT /B 0
lit
  • 14,456
  • 10
  • 65
  • 119
  • Mogsdad the solution provided by u is excellent... and now I am in need of one more requirement i.e. after this merging I need a new column saying S.No. and it should contain values starting from 1,2,3.... to number of rows – Prasad Oct 26 '15 at 12:35
  • like S.No. 1 2 3 4 5 . . . . . . . n – Prasad Oct 26 '15 at 12:37
  • @Prasad - I have not tested this change. Does this work for you? – lit Oct 26 '15 at 14:23
  • @Liturgist - Actually its not working... script looks good, seems it require little bit of changes. Working on that..... – Prasad Oct 27 '15 at 10:52
  • @Mogsdad - could u pls hlp me with the new requirement, which I have mentioned in above comments...Thanks in advance – Prasad Oct 29 '15 at 11:38
  • @Liturgist I had to remove `TYPE` but the script works fine. I wanted it to work for files starting with the letters "EVN". But when changing the script to `EVN*.csv` nothing is happening. Any ideas why? – mafraqs Dec 07 '15 at 08:47
  • @Flouks - I just saw this again and it was missing a backtick on the TYPE command. I edited the answer. – lit Dec 14 '15 at 19:30