Merging text files in a folder to a single text file
Moderator: DosItHelp
-
- Posts: 9
- Joined: 29 Dec 2012 23:58
Merging text files in a folder to a single text file
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 !
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
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"
-
- Posts: 9
- Joined: 29 Dec 2012 23:58
Re: Merging text files in a folder to a single text file
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 ?
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
The batch file as it stands expects the files in the same directory.
This will change the folder first.
This will change the folder first.
Code: Select all
@echo off
cd /d "d:\textfiles"
copy *.txt temp.tmp
ren temp.tmp "Allfiles.txt"
-
- Posts: 9
- Joined: 29 Dec 2012 23:58
Re: Merging text files in a folder to a single text file
thanks a lot ! will work on this and post a reply soon.
-
- Posts: 9
- Joined: 29 Dec 2012 23:58
Re: Merging text files in a folder to a single text file
Thanks Foxidrive. The code works like a charm.
Re: Merging text files in a folder to a single text file
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
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.
-
- Posts: 9
- Joined: 29 Dec 2012 23:58
Re: Merging text files in a folder to a single text file
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.
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
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.
-
- Posts: 9
- Joined: 29 Dec 2012 23:58
Re: Merging text files in a folder to a single text file
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 !