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

2

1 Answer 1

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
7
  • 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, 2015 at 12:35
  • like S.No. 1 2 3 4 5 . . . . . . . n
    – Prasad
    Oct 26, 2015 at 12:37
  • @Prasad - I have not tested this change. Does this work for you?
    – lit
    Oct 26, 2015 at 14:23
  • @Liturgist - Actually its not working... script looks good, seems it require little bit of changes. Working on that.....
    – Prasad
    Oct 27, 2015 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, 2015 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.