Merging text files in a folder to a single text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
radiantracy
Posts: 9
Joined: 29 Dec 2012 23:58

Merging text files in a folder to a single text file

#1 Post by radiantracy » 30 Dec 2012 00:10

Hi all,

My requirement is to read all the text files in a folder ( say there are 10 *.txt files in D:\TextFiles ) and merge all the text files to a single file ( say output.txt ).

The output file should be in such a way that the contents should be copied in the same order the file was read.

Can someone help me out !

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Merging text files in a folder to a single text file

#2 Post by foxidrive » 30 Dec 2012 00:16

If they all end with a CR/LF then you can use this

Code: Select all

@echo off
copy *.txt temp.tmp
ren temp.tmp "Allfiles.txt"

radiantracy
Posts: 9
Joined: 29 Dec 2012 23:58

Re: Merging text files in a folder to a single text file

#3 Post by radiantracy » 30 Dec 2012 00:25

sorry to ask..im very new to this batch commands.

The batch file is executed in a different folder from where the text files are present.
So the code you posted will work in that condition too ?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Merging text files in a folder to a single text file

#4 Post by foxidrive » 30 Dec 2012 00:35

The batch file as it stands expects the files in the same directory.


This will change the folder first.

Code: Select all

@echo off
cd /d "d:\textfiles"
copy *.txt temp.tmp
ren temp.tmp "Allfiles.txt"

radiantracy
Posts: 9
Joined: 29 Dec 2012 23:58

Re: Merging text files in a folder to a single text file

#5 Post by radiantracy » 30 Dec 2012 00:42

thanks a lot ! will work on this and post a reply soon.

radiantracy
Posts: 9
Joined: 29 Dec 2012 23:58

Re: Merging text files in a folder to a single text file

#6 Post by radiantracy » 30 Dec 2012 00:46

Thanks Foxidrive. The code works like a charm. :P

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Merging text files in a folder to a single text file

#7 Post by foxidrive » 30 Dec 2012 00:50

Glad to help. I missed your path in the request the first time.

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

Re: Merging text files in a folder to a single text file

#8 Post by Squashman » 30 Dec 2012 11:42

foxidrive wrote:Glad to help. I missed your path in the request the first time.

I didn't interpret his original request as such either. But I have a feeling there is more to this request based on the initial question. The OP needs to realize that the cmd prompt will list files in alphanumeric order and not numeric order.

The initial request makes it seem like they need to be in a specific order.

radiantracy
Posts: 9
Joined: 29 Dec 2012 23:58

Re: Merging text files in a folder to a single text file

#9 Post by radiantracy » 30 Dec 2012 12:03

Hi Squashman,

The code is working fine. The content in the txt files are copied and written to the new file based on the order in the directory.

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

Re: Merging text files in a folder to a single text file

#10 Post by Squashman » 30 Dec 2012 12:14

radiantracy wrote:Hi Squashman,

The code is working fine. The content in the txt files are copied and written to the new file based on the order in the directory.


It doesn't sound like you know that the cmd prompt lists files in a different order than Windows explorer does.

For example. Make ten files named 1.txt 2.txt etc.... then use the dir cmd to list those files in a cmd window then look at them sorted by name in Windows explorer.

radiantracy
Posts: 9
Joined: 29 Dec 2012 23:58

Re: Merging text files in a folder to a single text file

#11 Post by radiantracy » 30 Dec 2012 12:18

ya u r right but in my project the order of the files does not matter much. But the query u have raised also matters. working on it !

Post Reply