I would like to merge multiple CSV files, (all files are in the same format), available in a directory, and their file name should be added as the last field.
Since the field names are same for all the CSV files, the field names should not repeat. I would like to achieve this using batch scripting.
Below is the example:
File1------ C1 C2-- -- AR1 BR1 AR2 BR2 File2----- C1 C2-- --CR1 DR1 CR2 DR2
Output should be:
Merge File---------------- C1 C2 File Name--- --- ---------AR1 BR1 File1AR2 BR2 File1CR1 DR1 File2CR2 DR2 File2
I tried below code, it merges the files perfectly, without repeating the header. All I need to add is the file name of each file and add it as the last field in the merge file.
@ECHO OFFpushd C:\Users\Ak\Desktop\Test\New folderSET first=ySET newfile=Merge_CSV_HB_R5217476_REDReport.csvfor %%F in (*.csv) do IF NOT %%F==%newfile% ( if defined first ( COPY /y "%%F" %newfile% >nul set "first=" ) else ( FOR /f "skip=1delims=" %%i IN (%%F) DO >> %newfile% ECHO %%i ))