Combine CSV Files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Santy4tas
Posts: 1
Joined: 30 Jul 2019 20:14

Combine CSV Files

#1 Post by Santy4tas » 30 Jul 2019 20:37

Hello dear people,

I need to combine .csv files but remain the original name. I'll explain

I have various .csv files with the following names

01-15 Codes.csv
01-85 Codes.csv
01-30 Codes.csv

I made a search and I can achive it easly using the following code in a Batch File

Code: Select all

@echo off
copy *.csv Combined.csv
But I need something more complex. Because I need the code respect the name of the combined files.
For example, If I use the code

Code: Select all

@echo off
copy *.csv 01.csv
For the files above in the example, all the file will combine in one file called "01.csv" bis this name is because I edited it in the code. I need a code that rename the combined file based on the group of files.

For exmaple, I have the following files:
02-15 Codes.csv
02-85 Codes.csv
02-30 Codes.csv

I need the resulting file is called "02.csv"

Or if I have the following files:
07-15 Codes.csv
07-85 Codes.csv
07-30 Codes.csv

I need the resulting file is called "07.csv"

In other words, I need the the combined file remain the first to characters of on of the initial files.

I cand edit the batch code each time I'll combine the .csv files based on the name of the group of file, but that's not the idea.

I hope you can help me.

Thank you so much!

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: Combine CSV Files

#2 Post by Eureka! » 31 Jul 2019 04:44

If your CSV files are rather small ( couple of hundred lines each), this should do it:

Code: Select all

@setlocal
@for %%x in (*-*.csv) DO @(set COMBI=%%x & call type %%x >>%COMBI:~0,2%.csv)
For larger files, the copy syntax copy fileA + fileB is probably faster.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Combine CSV Files

#3 Post by penpen » 03 Aug 2019 07:13

On windows 10 you also could use something like that (untested):

Code: Select all

@echo off
setlocal DisableDelayedExpansion
for /f %%a in ('
	"((for /f "delims^=-" %%b in ('dir /b "*-*.csv"') do @echo(%%~b) | sort /unique)"
') do (
	copy "%%~a-*.csv" "%%~a.csv"
)

endlocal
goto :eof
penpen

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: Combine CSV Files

#4 Post by Eureka! » 03 Aug 2019 10:20

penpen wrote:
03 Aug 2019 07:13

Code: Select all

sort /unique
:shock: Thank you!!
I was using a 3rd party SORT especially for that.
Thanks for mentioning that undocumented feature.


Yet another way (of the many) to approach this:

Code: Select all

@echo off
setlocal enabledelayedexpansion

for %%x in (*-*.csv) do (
  set COMBI=%%x
  if not exist "!COMBI:~0,2!.csv"  copy /b "!COMBI:~0,2!-*.csv"  "!COMBI:~0,2!.csv"
)

endlocal
For - for example - 07-15 Codes.csv this would expand to:
if not exist "07.csv" copy /b "07-*.csv" "07.csv"

That way all 07-*.csv will be copied in one run, instead of many copy actions.
( I addded the /b switch to the copy command btw)

Post Reply