GeekPolice Tech TutorialsLog in

 


How to Combine or Merge Multiple Text Files into Single Text File

Share

20180217
How to Combine or Merge Multiple Text Files into Single Text File

How to Combine or Merge Multiple Text Files into Single Text File
There may be occasions where you may need to merge multiple text files into single text file.

In this tutorial, I’ll write about several ways to combine text files into single text file.

First thing to do is to open a Command Prompt by clicking Start Menu > All Programs > Accessories > Command Prompt. In Windows Vista and later versions of Window operating systems type cmd.exe into Start Screen or Start Menu, click on Cmd.exe, to open Command Prompt.

We need to change the Command Prompt working directory path to the directory path where the text files are stored.

The CD command can be use to change the Command Prompt working directory path to the directory path where the text files are stored.

For example, lets say the text files are stored at "C:\MyFiles" directory, at the Command Prompt type following command and press Enter key.

Code:

CD C:\MyFiles


If the folder name contains a space surround the folder name with quotes, as shown below.

Code:

CD "C:\My Files"


Batch script posted at below thread, can be use to add a 'Open Command Prompt Window Here' context menu option. Which can be use to open a Command Prompt for any folder or drive.

Add 'Open Command Prompt Window Here' Context Menu Option in Windows

How to Combine or Merge Multiple Text Files into Single Text File 2NtZHT0

As you can see above, I have three text documents in the folder along with a one subfolder. If I only want to combine the text files in this one folder, I would issue these two commands:

Code:

CD C:\MyFiles

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


How to Combine or Merge Multiple Text Files into Single Text File D2zXvA2

This is a simple FOR loop that loops through all the files with .TXT extension and outputs them to a file called output.txt and stores this file in c:\Test folder if exist.

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 file extension in the For 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.

If you have text files that are located not just in one folder, but in many subfolders.  Execute following commands, which will tell For command to recursively search for text files in any subfolders of the current directory.

Code:


CD C:\MyFiles

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


How to Combine or Merge Multiple Text Files into Single Text File MZxz8wS

There is a actually another command called Copy, that allows you to do the same thing as the FOR statement above.

Code:

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.
remove_circleSimilar topics

Comments

No Comment.
Permissions in this forum:
You cannot reply to topics in this forum