There are several cases where you might need to combine multiple text files into one text file. For example, you can get a CD containing hundreds of text files in different directories, which you need to combine into one file for import to Excel, etc.
It is also useful if you have network log files, server log files, or backup logs that you want to combine for data mining or data analysis purposes. There are several different ways to combine text files, and the results may vary slightly depending on the method you choose.
In this article, I’ll write about several ways to combine text files, so if one method doesn’t work too well, you can try something else.
First method – command prompt
If you are ok with using the command line, then there are a few simple commands you can use to quickly merge an entire group of text files. The advantage of using the command line is that you don’t need to install any third party software. If you’d like a little tutorial on using the command line, check out my beginner’s guide to using the command line.
Also, since the command line can take multiple parameters, you can actually create a rather complex command to filter and sort the files that you want to include in the attach process. I’ll explain the simplest command, but I’ll also go through some examples to show you how to do more complex things.
First, open Windows Explorer and navigate to the directory where the text files are located. If the files are stored in many subfolders, change to the parent directory. Now press and hold CTRL + SHIFT and then right-click any empty space in the explorer window.
This will open up a command window that is already set to the directory you were in. Now all we have to do is enter the command. As you can see above, I have three text documents and a couple of folders in my folder. If I want to concatenate only text files in this one folder, I would run this command:
for% f in (* .txt) enter “% f” >> c: Test output.txt
In programming language, this is a simple FOR loop that loops through all files ending in .TXT and outputs them to a file called output.txt.
As you can see above, the loop simply runs a separate command for each text file it finds in the directory. Please note, if you have a text file, but it has a different extension like .log, .dat, etc., then you can simply change the value of * .txt in the command. It’s also worth noting that the output must not be in the current directory, otherwise the output file will be appended to itself, since it is also a text file.
Let’s say you have text files that are not only in one folder, but in many subfolders. In this case, we can add a parameter to the command that tells it to recursively search for text files in any subfolders of the current directory.
for / R% f in (* .txt) enter “% f” >> c: Test output.txt
You will notice the / R option just after the for statement. Now when I run the command, you will see that it finds a couple of additional text files in three directories under the same directory.
As usual on the command line, there is actually another command that allows you to do the same as the FOR statement above. Actually this command is much simpler and if it suits you you can use it instead of the above method.
copy * .txt output.txt
This command works well, but it has fewer parameters than the previous one. For example, it will prevent you from recursively searching through subfolders.
The second method – TXTCollector
TXTCollector is a free text file merger tool with a decent set of features. It is very easy to use and can be configured to work in several ways.
First, enter or copy and paste the path to the folder in the “Folder” field at the top, or just click the “Browse folders” button and select the folder with the text files. Then you can choose which type of files you want to combine.
By default TXTCollector will search all TXT files and merge them. However, you can choose from the list and merge or merge multiple CSV, BAT, HTM, LOG, REG, XML and INI files into one!
Then you can choose the separator to be displayed between each merged file. This is a nice feature that is not available on the command line. You can choose an option from the drop-down menu, or just type whatever you want in the box.
By default, the program places the directory name, file name and separator between each file. If you want to merge files continuously without gaps between each file, select the No Separator, No File Name, and No Carriage Return check boxes.
After that, you will have a choice: add a space between files or not. The great thing about TXTCollector is that you can actually customize it. If you click on the link at the bottom called Extensions and Separators, you can add your own extensions to TXTcollector.
Edit the extensions.txt file located in the TXTCollector application data directory. Note that TXTcollector only handles plain text files, no matter which extension is used. Hence, it cannot combine multiple XLS files, for example, if they are not saved as plain text.
The only limitation of the program is that it can simultaneously combine no more than 32,765 text files. If you have more, you can combine that number into one, and then combine the larger with more smaller ones, up to 32,765!
All in all, a very simple yet powerful free application for combining multiple text files. Hopefully these two methods will work for most people. If you are faced with a more difficult situation, feel free to leave comments and I will try to help.
Also, be sure to check out my other post on how to combine multiple PowerPoint presentations Enjoy!