?
Solved

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

Posted on 2011-05-12
4
Medium Priority
?
1,125 Views
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

0
Comment
Question by:urfried
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 1400 total points
ID: 35744512
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

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35744808
Have you tried doing just:

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

or are they all from different subdirs?

Steve
0
 

Author Closing Comment

by:urfried
ID: 35744928
Great... easy and fast solution.
0
 

Author Comment

by:urfried
ID: 35744929
Thanx a lot.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month16 days, 12 hours left to enroll

862 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