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

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

Who is Participating?
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

Steve KnightIT ConsultancyCommented:
Have you tried doing just:

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

or are they all from different subdirs?

urfriedAuthor Commented:
Great... easy and fast solution.
urfriedAuthor Commented:
Thanx a lot.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.