batch cmd merge csv files copy /b - !count! for /f output

Posted on 2011-05-12
Last Modified: 2012-06-22
Ola gents,

Weekly about 150 csv files need to be merged into one csv file.
I have created a batch that works but is way too long.
Looking for a good solution.

copy /b file1+file2+file3 export.csv  will give me the result.
See below code i have now.

All help is appreciated.
setlocal enabledelayedexpansion

FOR /f "tokens=*" %%A IN ('dir /b /s *.csv ') DO (
							set /a Count+=1
							set R!Count!="%%A"
if !count!==1  set echo=!R1! 
if !count!==2  set echo=!R1!+!R2! 
if !count!==3  set echo=!R1!+!R2!+!R3! 
if !count!==4  set echo=!R1!+!R2!+!R3!+!R4! 
if !count!==5  set echo=!R1!+!R2!+!R3!+!R4!+!R5! 
if !count!==6  set echo=!R1!+!R2!+!R3!+!R4!+!R5!+!R6! 
if !count!==7  set echo=!R1!+!R2!+!R3!+!R4!+!R5!+!R6!+!R7! 
if !count!==8  set echo=!R1!+!R2!+!R3!+!R4!+!R5!+!R6!+!R7!+!R8! 
if !count!==9  set echo=!R1!+!R2!+!R3!+!R4!+!R5!+!R6!+!R7!+!R8!+!R9! 
if !count!==10 set echo=!R1!+!R2!+!R3!+!R4!+!R5!+!R6!+!R7!+!R8!+!R9!+!R10! 

rem .........
rem enz. enz... - batch goes to "if !count!==50" right now... is way to long.

echo ^( copy /b  !echo! export.csv ^) >> export.bat
call export.bat

Open in new window

Question by:urfried
    LVL 82

    Accepted Solution

    Try this; it's currently in test mode and will only display the "copy" command it would normally run. Remove the capitalized ECHO to run it for real. Note that the maximum length of a command line is about 8000 characters.
    @echo off
    setlocal enabledelayedexpansion
    set CombinedFile=export.csv
    set cl=
    for /f "delims=" %%a in ('dir /b /s *.csv') do (
      set cl=!cl!+"%%~a"
    set cl=%cl:~1%
    ECHO copy /b !cl! "%CombinedFile%"

    Open in new window

    LVL 43

    Expert Comment

    by:Steve Knight
    Have you tried doing just:

    copy /a *.csv out.txt
    copy *.csv out.txt

    or are they all from different subdirs?


    Author Closing Comment

    Great... easy and fast solution.

    Author Comment

    Thanx a lot.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    DOS FTP 9 205
    Need honest data/inout on FAX Appliances 2 61
    Rename files on Windows 10 35
    Build VS2015 project in bat command 3 55
    TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
    YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT ( The crux of this batch f…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now