Subscribe:

Ads 468x60px

Pages

samedi 29 août 2015

Computer Tips From A Computer Guy

Computer Tips From A Computer Guy


How to Combine or Merge Multiple Text Files

Posted: 28 Aug 2015 11:49 AM PDT

There are several occasions where you may need to merge multiple text files into single text file. For example, you may receive a CD that contains hundreds of text files, all in different directories, which you need to combine into one file for importing into Excel, etc.

It's also useful if you have network log files, server log files, or backup logs that you want to combine for purposes of data mining or data analysis. There are a couple of different ways you can go about joining text files together and the results are slightly different depending on the method you choose.

In this article, I’ll write about several ways to combine text files so that if one method doesn’t work out too well, you can try something else.

Method 1 – Command Prompt

If you are ok using the command prompt, then there are a couple of simple commands you can use to merge a whole bunch of text files quickly. The advantage of using the command prompt is that you don’t have to install any third-party programs. If you want a little primer on using the command prompt, check out my beginner’s guide to use the command prompt.

Also, since the command line can take multiple parameters, you can really create quite a complex command to filter and sort through which files you want to include in the joining process. I’ll explain the simplest command, but will also delve into a few examples to show you how to do the more complicated stuff.

Firstly, open Windows Explorer and go to the directory where you text files are located. If the files are stored in many subfolders, navigate to the parent directory. Now press and hold CTRL + SHIFT and then right-click on any empty spot in the Explorer window.

open command window here

This will open a command window that is already set to the directory you were in. Now all we have to do is type in the command. As you can see above, I have three text documents in the folder along with a couple of folders. If I only want to combine the text files in this one folder, I would issue this command:

for %f in (*.txt) do type “%f” >> c:\Test\output.txt

In coding parlance, this is a simple FOR loop that loops through all the files end with .TXT and outputs them to a file called output.txt.

combine txt cmd

As you can see above, the loop just runs a separate command for each text file that it finds in the directory. Note that if you have a text file, but it has a different extension like .log or .dat, etc, then you can simply change the *.txt value in the command. It’s also worth noting that the output should be to a different location than the current directory, otherwise it will append the output file to itself since it also is a text file.

Now let’s say you have text files that are located not just in one folder, but in many subfolders. In this case, we can add a parameter to the command, which will tell it to recursively search for text files in any subfolders of the current directory.

for /R %f in (*.txt) do type “%f” >> c:\Test\output.txt

You’ll notice the /R parameter right after the for statement. Now when I run the command, you’ll see that it finds a couple of extra text files in the three directories that are in the same directory.

merge text files

As is usual with the command prompt, there is actually another command that allows you to do the same thing as the FOR statement above. The command is actually a lot simpler and if it works fine for you, then feel free to use it instead of the above method.

copy *.txt output.txt

copy merge text files

This command works well, but doesn’t have as many options as the previous command. For example, it won’t let you recursively search through subfolders.

Method 2 – TXTCollector

TXTCollector is a free text file-merging tool with a decent feature set. It's very easy to use and can be configured to work in a couple of different ways.

combine text files

First, type or copy and paste the folder path into the Folder box at the top or simply click on Browse Folders button and select the folder with the text files. You can then choose which type of files you want to combine.

By default, TXTCollector will search for all TXT files and combine them. However, you can pick from the list and combine or merge multiple CSV, BAT, HTM, LOG, REG, XML, and INI files into one also!

Check the Include subfolders box if you want TXTCollector to recursively look into each sub-folder of the main folder. TXTCollector will show you exactly how many files it found in the directory.

merge txt files

Next you can choose a separator that will appear between each file that is being combined. This is a nice feature that you don’t get with the command line method. Either you can pick from the drop down menu or you can just type in whatever you want into the box.

By default, the program will put the directory name, file name, and the separator between each file. If you want to combine the files continuously without any break between each file, check off No Separator, No Filename, and No Carriage Returns.

combine multiple txt files

You will then have the choice of adding a space character between the files or not. The cool thing about TXTCollector is that you can really 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 what extension is used. Therefore, it cannot combine multiple XLS files, for example, unless they are saved as plain text.

The only limitation to the program is that it can only combine 32,765 text files at once. If you have more than that, you can combine that many into one and then combine the large one with more smaller ones, up to 32,765!

Overall, a very simple, yet powerful freeware app for combining multiple text files. Hopefully, these two methods will work for most people. If you have run into a situation that is more complicated, feel free to post a comment and I’ll try to help.

Also, be sure to check out my other post on how to combine multiple PowerPoint presentations. Enjoy!

The post How to Combine or Merge Multiple Text Files appeared first on Online Tech Tips.

0 commentaires:

Enregistrer un commentaire