CSV Collector Collector Remove Header

I have several CSV files with the same header, and I'm trying to merge them together in Batch and save only one header. Any ideas?

+6
source share
2 answers

You can use MORE +1 to display all but the first 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 merge all csv files in the current folder: Edit: changed to not use the newly created csv output 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" ) ) move /y new.csv.tmp new.csv >nul 

Or you can use FOR / F to avoid processing the newly created file:

 @echo off setlocal set first=1 >new.csv ( for /f "eol=: delims=" %%F in ('dir /b /ad *.csv') do ( if defined first ( type "%%F" set "first=" ) else more +1 "%%F" ) ) 

Obviously, this only works if all the csv files are in the same format.

EDIT 2015-07-30: There are some limitations:

  • Tabs will be converted to spaces
  • Each source CSV file must contain less than 64 thousand lines
+7
source

I am having problems with the dbenham method to merge all CSV files in the current folder. Sometimes he collected the resulting CSV and included it in the kit. I changed 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" ) ) ) 
+3
source

Source: https://habr.com/ru/post/927023/


All Articles