Page 1 of 1
merge multiple text files into one text file
Posted: 05 Apr 2019 05:47
by wasimsono
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.
Re: merge multiple text files into one text file
Posted: 05 Apr 2019 06:04
by aGerman
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
Re: merge multiple text files into one text file
Posted: 05 Apr 2019 06:36
by wasimsono
If I exclude then how can I mention the new file name, which is "DataFile".
Re: merge multiple text files into one text file
Posted: 05 Apr 2019 06:45
by aGerman
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
Re: merge multiple text files into one text file
Posted: 05 Apr 2019 07:02
by wasimsono
Ok. aGerman. I understand. I just excluded .txt from the code and its working i.e. no duplication. Thanks a lot.
Re: merge multiple text files into one text file
Posted: 05 Apr 2019 07:55
by Squashman
This should be quicker.
Code: Select all
copy /b *.txt Datafile.tmp & ren Datafile.tmp DataFile.txt