I have multiple csv
files coming on daily basis from a different server. These files are huge(over 200 MB). I have to remove header for all these csv
files
and replace them with required column headers using batch file.
The below code works fine to remove the column headers from one single file only:
@echo off
set "csv=mycsv.csv">"%csv%.new"
(
for /f skip^=1^ usebackq^ delims^=^ eol^= %%A in ("%csv%") do echo %%A
)
move /y "%csv%.new" "%csv%" >nul
Given that the CSV files do not contain any TAB characters (which were replaced by sequences of SPACE characters by the used
more
command) and that no file is longer than 65534 lines (in which casemore
expects user interaction), you could try one of the following:The new column header is given by another file
headerfile.csv
:You might not want to exclude
headerfile.csv
from being processed in case it is not located in the current directory where all the other CSV files are; simply remove theif
query then.The new column header is given as a string constant:
Update
Here is a way without using the
more
command, so its limitations do no longer apply. It does also not usefor /F
which would limit the length of each line to 8191 bytes/characters:The new column header is given by another file
headerfile.csv
:The new column header is given as a string constant:
Note that the header line is still limited to 8191 bytes/characters, because it is stored in a variable (in order to avoid multiple file read operations), and also by the related
echo(%HEADER%
command line which is also limited to that size. To overcome this limit, place only the header into a text file and with in the loop, copy it to%%~F.tmp
prior to appending the data.You may even get away with using MORE:
Note - This method will convert any tabs to spaces
should do what you want, using the ubiquitous crystal ball to scry "required column headers" to mean "the column headers from a .csv file"