在某些情况下,您可能需要将多个文本文件(text file)合并为一个 文本文件(text file)。例如,您可能会收到一张包含数百个文本文件(text file)的 CD,它们都位于不同的目录中,您需要将这些文件合并为一个文件以导入Excel等。
如果您想要将网络日志(network log)文件、服务器日志文件或备份日志组合起来用于数据挖掘或数据分析(mining or data analysis),它也很有用。有几种不同的方法可以将文本文件连接在一起,根据您选择的方法,结果会略有不同。
在本文中,我将介绍几种组合文本文件的方法,以便如果一种方法(method doesn)效果不佳,您可以尝试其他方法。
方法 1 – 命令提示符
如果您可以使用命令提示符(command prompt),那么您可以使用几个简单的命令来快速合并一大堆文本文件。使用命令提示符(command prompt)的好处是您不必安装任何第三方程序。如果您想了解如何使用命令提示符(command prompt),请查看我的初学者指南(s guide)以使用命令提示符(command prompt)。
此外,由于命令行(command line)可以采用多个参数,因此您实际上可以创建一个相当复杂的命令来过滤和排序(filter and sort)要包含在加入过程(joining process)中的文件。我将解释最简单的命令,但也会深入研究一些示例,向您展示如何执行更复杂的操作。
首先,打开Windows 资源管理器(Windows Explorer)并转到文本文件所在的目录。如果文件存储在许多子文件夹中,请导航到父目录(parent directory)。现在按住CTRL + SHIFT,然后右键单击资源管理器窗口(Explorer window)中的任何空白点。
这将打开一个已经设置为您所在目录的命令窗口(command window)。现在我们所要做的就是输入命令。正如您在上面看到的,我在文件夹中有三个文本文档以及几个文件夹。如果我只想合并这个文件夹中的文本文件,我会发出这个命令:
for %f in (*.txt) do type “%f” >> c:\Test\output.txt
用编码术语来说,这是一个简单的FOR循环,它遍历所有以.TXT结尾的文件,并将它们输出到一个名为output.txt的文件中。
正如您在上面看到的,循环只是为它在目录中找到的每个文本文件运行一个单独的命令。(text file)请注意,如果您有一个文本文件(text file),但它具有不同的扩展名,例如.log 或 .dat(.log or .dat)等,那么您可以简单地更改命令中的 *.txt 值。还值得注意的是,输出应该位于与当前目录不同的位置,否则它会将输出文件(output file)附加到自身,因为它也是一个文本文件(text file)。
现在假设您的文本文件不仅位于一个文件夹中,而且位于许多子文件夹中。在这种情况下,我们可以在命令中添加一个参数,这将告诉它递归搜索当前目录的任何子文件夹中的文本文件。
for /R %f in (*.txt) do type “%f” >> c:\Test\output.txt
您会在 for 语句之后注意到/R现在,当我运行该命令时,您会看到它在同一目录中的三个目录中找到了几个额外的文本文件。
与命令提示符(command prompt)一样,实际上还有另一个命令允许您执行与上述FOR 语句相同的操作。该命令实际上要简单得多(lot simpler),如果它适合您,请随意使用它来代替上述方法。
copy *.txt output.txt
此命令运行良好,但没有上一个命令那么多的选项。例如,它不会让您递归搜索子文件夹。
方法 2 – TXTCollector
TXTCollector是一个免费的文本文件合并工具,具有不错的功能集。它非常易于使用,并且可以配置为以几种不同的方式工作。
首先,键入或复制(type or copy)文件夹路径(folder path)并将其粘贴到顶部的文件夹框中,或者只需单击(Folder)浏览文件夹(Browse Folders) 按钮并选择包含文本文件的文件夹。然后,您可以选择要合并的文件类型。
默认情况下,TXTCollector将搜索所有TXT文件并将它们合并。但是,您可以从列表(list and combine)中选择并将多个CSV、BAT、HTM、LOG、REG、XML和INI文件合并或合并为一个!
如果您希望TXTCollector递归查看主文件夹的每个子文件夹,请选中包含子文件夹框。(Include subfolders)TXTCollector将准确显示它在目录中找到的文件数量。
接下来,您可以选择将出现在每个正在合并的文件之间的分隔符。这是命令行方法所(command line method)没有(t get)的一个很好的特性。您可以从下拉菜单中选择,也可以在框中输入您想要的任何内容。
默认情况下,程序会在每个文件之间放置目录名(directory name)、文件名(file name)和分隔符。如果您想连续合并文件,每个文件之间没有任何中断,请选中No Separator、No Filename和No Carriage Returns。
然后,您可以选择是否在文件之间添加空格字符(space character)。TXTCollector 最酷(TXTCollector)的地方在于您可以真正自定义它。如果您单击底部名为Extensions and Separators的链接,您可以将自己的扩展添加到TXTcollector。
编辑位于TXTCollector 应用程序(TXTCollector application)数据目录中的extensions.txt文件。(extensions.txt)请注意,无论使用什么扩展名, TXTcollector只处理纯文本文件。(plain text)因此,它不能合并多个XLS文件,例如,除非它们保存为纯文本(plain text)。
该程序的唯一限制是它一次只能合并 32,765 个文本文件。如果你有更多,你可以把那么多合二为一,然后把大的和更小的结合起来,最多 32,765!
总体而言,这是一个非常简单但功能强大的免费软件应用程序(freeware app),用于组合多个文本文件。希望(Hopefully)这两种方法适用于大多数人。如果您遇到更复杂的情况,请随时发表评论,我会尽力提供帮助。
此外,请务必查看我关于如何组合多个PowerPoint演示文稿的另一篇文章。享受!
How to Combine or Merge Multiple Text Files
There are several ocсаsions where yoυ may need to merge mυltiplе text files into sіngle text file. For examрle, yоu may receive a CD that contains hundreds of text files, all in different directоries, whiсh you need to combine into onе 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.
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.
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.
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
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.
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.
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.
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!