Page 1 of 1
Merging text files in a folder to a single text file
Posted: 30 Dec 2012 00:10
by radiantracy
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 !
Re: Merging text files in a folder to a single text file
Posted: 30 Dec 2012 00:16
by foxidrive
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"
Re: Merging text files in a folder to a single text file
Posted: 30 Dec 2012 00:25
by radiantracy
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 ?
Re: Merging text files in a folder to a single text file
Posted: 30 Dec 2012 00:35
by foxidrive
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"
Re: Merging text files in a folder to a single text file
Posted: 30 Dec 2012 00:42
by radiantracy
thanks a lot ! will work on this and post a reply soon.
Re: Merging text files in a folder to a single text file
Posted: 30 Dec 2012 00:46
by radiantracy
Thanks Foxidrive. The code works like a charm.
Re: Merging text files in a folder to a single text file
Posted: 30 Dec 2012 00:50
by foxidrive
Glad to help. I missed your path in the request the first time.
Re: Merging text files in a folder to a single text file
Posted: 30 Dec 2012 11:42
by Squashman
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.
Re: Merging text files in a folder to a single text file
Posted: 30 Dec 2012 12:03
by radiantracy
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.
Re: Merging text files in a folder to a single text file
Posted: 30 Dec 2012 12:14
by Squashman
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.
Re: Merging text files in a folder to a single text file
Posted: 30 Dec 2012 12:18
by radiantracy
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 !