Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
wasimsono
- Posts: 11
- Joined: 03 Apr 2017 06:06
#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: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#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
#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: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#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
#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: 4486
- Joined: 23 Dec 2011 13:59
#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