Programming
text batch-file merge batch-processing skip
Updated Wed, 25 May 2022 21:02:55 GMT

merge many txt file contents and skip first line in batch command file


I want to create a Batch command file to merge text file with extension ".mf" However, each file contains date in the first line, which I donot want in the final output file, Please advice how do I get rid of the date line from each file while mergingin into one big txt file.

I have used the following command for merging the txt files for a batch file.

copy *.mf big.one
ren big.one filename.mf

Example:

2013218;
a
b
c
d

-

2013218;
u
v
w
x
y
z

The output must be like below:

2013218;
a
b
c
d
u
v
w
x
y
z

The sorting does not matter.




Solution

@echo off
del big.one 2> NUL
for %%f in (*.mf) do (
   if not exist big.one (
      copy "%%f" big.one
   ) else (
      for /F  "usebackq skip=1 delims=" %%a in ("%%f") do (
         echo %%a>> big.one
      )
   )
)
set /P fileDate=< big.one
ren big.one filename_%fileDate:~0,-1%.mf

This solution does not preserve empty lines from second file on; this may be fixed, if needed.





Comments (2)

  • +0 – Thankx Aacini, It worked perfectly fine. I can now save the manual work I do everyday. just a another small query regarding the file created above. I want the file to be saved as <filename>__<YYYYMMDD>.mf Where the date & format is same as in the first line of the file. — Feb 19, 2013 at 11:04  
  • +0 – I have a similar process, where I have to merge many files in .txt extension. But this command does not work well. The file is created correctly, but the output file does not have extension ".txt" — Aug 28, 2013 at 05:57