Home » Tech Tips » Windows » How to Merge Multiple Text Files on Windows?

How to Merge Multiple Text Files on Windows?

Have you ever needed to combine several text files into one on your Windows PC? Whether you are compiling logs, stitching together chapters, or merging data exports from colleagues, copy-pasting is slow, tedious, and prone to mistakes. Fortunately, Windows offers multiple built-in command-line tools with advanced scripting options to merge text files in seconds.

Prerequisites for File Merging

Before we dive in, make sure you’re prepared:

  • All files should be plain text (.txt) and use the same encoding (preferably UTF‑8). Mixed encodings can produce garbled characters.
  • If your files have headers or need chronological order, sort them beforehand. Consider adding a separator line inside the content (like —) to distinguish sections.
  • Always keep copies of originals. After merging, open the result to check for corruption or formatting issues.

We will merge three example files (1.txt, 2.txt, 3.txt) located in C:\Users\<Username>\Downloads\MyFiles into a single merged.txt or new-merged.txt file.

Example Text Files in Downloads Folder

1. Classic Command Prompt (CMD)

The copy command is the oldest and simplest way to merge files in Windows.

  • Press Win + R, type cmd, and press “OK” to open Command Prompt.
  • Navigate to your folder using cd command. In our example, we will use the following command.
cd C:\Users\<Username>\Downloads\MyFiles
  • To merge all .txt files in alphabetical order, run the following command:
copy *.txt merged.txt
Files Copied Successfully
  • You will see a success message showing “1 file(s) copied” and you can find the merged.txt file in the same location (Downloads\MyFiles). The file’s content will be concatenated in alphabetical order by name (1.txt, 2.txt, 3.txt).
View Merged File
  • To set a custom order, list the files explicitly as in the below command. This merges 3.txt, then 2.txt, then 1.txt into new-merged.txt.
copy 3.txt + 2.txt + 1.txt new-merged.txt

2. Merge Files Using PowerShell

PowerShell gives you better control over encoding and large files.

  • Right-click the Start button and select “Terminal (Admin)” menu. When you are in the Terminal app, click the small arrow on the title bar and select Windows PowerShell to use that interface.
  • First, change the prompt to required folder using CD command.
  • Type the following command and press enter to merge specific files in sequence:
Get-Content 1.txt, 2.txt, 3.txt | Set-Content merged.txt
Merge Files with PowerShell
  • You will NOT see any confirmation after running the command. However, the new merged.txtfile will be created in the same folder. You can change the file name sequence in the command to change the concatenation order.
  • Use the following command for adding a separator between files. This inserts —- after every file including the last.
Get-Content 1.txt, 2.txt, 3.txt | ForEach-Object { $_; "----" } | Set-Content merged.txt

Remember that PowerShell sometimes uses UTF‑16 by default. Use the below command to force UTF‑8:

Get-Content -Encoding UTF8 1.txt, 2.txt, 3.txt | Set-Content -Encoding UTF8 merged.txt

Similarly, to avoid memory issues when merging larger files, use -ReadCount:

Get-Content -ReadCount 1000 *.txt | Set-Content merged.txt

3. Using a Python Script for File Merging

If you frequently merge dozens of files, a Python script is a lifesaver. Since this is platform independent, you can use the same script on macOS also.

  • Install Python (if not already) from python.org.
  • Save this script as merge.py in your folder:
import glob

files_to_merge = ['1.txt', '2.txt', '3.txt']  # or use glob.glob('*.txt')
with open('merged.txt', 'w', encoding='utf-8') as outfile:
    for fname in files_to_merge:
        with open(fname, 'r', encoding='utf-8') as infile:
            outfile.write(infile.read().rstrip() + '\n')  # ensure newline
        outfile.write('--- separator ---\n')  # optional
  • Run it from CMD/PowerShell with the following command:
python merge.py

The advantage here is that you can sort files by date, filter by pattern, or add complex logic easily.

4. Merging Files Using a Batch Script

  • Save the following as merge_all.bat in your folder:
@echo off
set output=merged.txt
if exist %output% del %output%
for %%f in (*.txt) do (
    type "%%f" >> %output%
    echo. >> %output%
)
echo Merging complete.
  • Double‑click the .bat file any time to refresh the merge, which is best for repetitive tasks.

Final Thoughts

For small number of files, you can make use of the Command Prompt or PowerShell. If you need to frequently merge large number of files, then creating a custom script can help to save lot of time. There are also lot of online tools available for merging text files. However, we don’t recommend using them as the uploaded files will be retained on their server creating security and privacy risks for you.

Leave a Comment

Your email address will not be published. Required fields are marked *