merge multiple text files into one text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
wasimsono
Posts: 11
Joined: 03 Apr 2017 06:06

merge multiple text files into one text file

#1 Post by wasimsono » 05 Apr 2019 05:47

I need to merge about 3000 text files into one file. I found following command and used it. Its working fine.

Code: Select all

for %f in (*.txt) do type "%f" >> Datafile.txt

but the problem is that it creates duplication of data. For example I have two text files which have 8 and 3 lines of data simultaneously. when I run the command it creates a file which having 36 lines of data. I don't know how is it happening.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: merge multiple text files into one text file

#2 Post by aGerman » 05 Apr 2019 06:04

I don't know if this causes your problem but remember that Datafile.txt has extension *.txt, too. You should exclude it.

Code: Select all

for %f in (*.txt) do if /i "%~f" neq "Datafile.txt" type "%f">>"Datafile.txt"
Steffen

wasimsono
Posts: 11
Joined: 03 Apr 2017 06:06

Re: merge multiple text files into one text file

#3 Post by wasimsono » 05 Apr 2019 06:36

If I exclude then how can I mention the new file name, which is "DataFile".

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: merge multiple text files into one text file

#4 Post by aGerman » 05 Apr 2019 06:45

I don't understand your question. I just try to tell you that you should avoid to append the data already written to "Datafile.txt" once more. For that reason "Datafile.txt" has to be excluded from the files that the FOR loop found. Just have a look at the IF statement in my code.

Steffen

wasimsono
Posts: 11
Joined: 03 Apr 2017 06:06

Re: merge multiple text files into one text file

#5 Post by wasimsono » 05 Apr 2019 07:02

Ok. aGerman. I understand. I just excluded .txt from the code and its working i.e. no duplication. Thanks a lot.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: merge multiple text files into one text file

#6 Post by Squashman » 05 Apr 2019 07:55

This should be quicker.

Code: Select all

copy /b *.txt Datafile.tmp & ren Datafile.tmp DataFile.txt

Post Reply