I have multiple CSV files with the same header and I'm trying to combine them together in Batch and keep only a single header. Any ideas?
You could use MORE +1
to output all but the 1st line.
>new.csv (
type file1.csv
more +1 file2.csv
more +1 file3.csv
REM etc.
)
Obviously you can adjust the number of lines to skip in each file as needed.
To combine all csv files in the current folder: Edit: modified to not use newly created output csv as input
@echo off
setlocal
set first=1
>new.csv.tmp (
for %%F in (*.csv) do (
if defined first (
type "%%F"
set "first="
) else more +1 "%%F"
)
)
ren new.csv.tmp new.csv
Obviously this is only effective if all the csv files share the same format.
EDIT 2015-07-30: There are some limitations:
I was having issues with dbenham's method for combining all CSV files in the current folder. It would occasionally pick up the resulting CSV and include it in the set. I have modified it to avoid this problem.
@echo off
setlocal
set first=1
set fileName="combinedFiles.csv"
>%fileName% (
for %%F in (*.csv) do (
if not "%%F"==%fileName% (
if defined first (
type "%%F"
set "first="
) else more +1 "%%F"
)
)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With