How to Combine (Concatenate) Multiple Text Files in Batch Script
Combining multiple text files into a single, consolidated file is a common data processing task. You might need to merge a series of log files into one master log, combine partial CSV reports into a monthly summary, or simply join several text documents. Windows Batch provides a few straightforward, built-in methods to accomplish this, with the COPY command being the most direct and powerful tool for the job.
This guide will teach you how to use the COPY command with wildcards and addition syntax to merge files. We will also cover the critical pitfall of including your output file in your input selection and provide an advanced practical script for combining CSV files while correctly handling header rows.
The Primary Method: The COPY Command
The COPY command has a built-in feature that allows it to concatenate (or "add") files together. You can do this in two ways:
Method 1: Using Wildcards (*)
This is the simplest method for combining all files that match a pattern.
COPY *.log "all_logs.log"
Method 2: Using the Plus (+) Sign
This method gives you explicit control over which files are included and in what order.
COPY "file1.txt" + "file2.txt" + "file3.txt" "combined.txt"
This "addition" syntax tells COPY to read the source files in sequence and write their contents to the destination file.
An Alternative Method: Using TYPE in a FOR Loop
For more complex scripting, you can iterate through files with a FOR loop and append their contents to a destination file using the TYPE command and the append operator (>>).
@ECHO OFF
SET "DEST_FILE=all_logs.log"
IF EXIST "%DEST_FILE%" DEL "%DEST_FILE%"
FOR %%F IN (*.log) DO (
TYPE "%%F" >> "%DEST_FILE%"
)
This method is more verbose but can be integrated into more complex loops where other logic is also being performed on each file.
Basic Example: Merging Log Files
Let's combine a set of daily log files into a single master log for the week.
Consider the following source files: ``txt title="2023-10-25.log" INFO: Service started. WARN: Connection timeout.
```txt title="2023-10-26.log"
INFO: Service started.
INFO: User 'admin' logged in.
The wildcard COPY command is perfect for the following script:
@ECHO OFF
ECHO Combining all .log files into weekly_report.log...
COPY *.log weekly_report.log
ECHO Done.
And the resulting weekly_report.log file is the following:
INFO: Service started.
WARN: Connection timeout.
INFO: Service started.
INFO: User 'admin' logged in.
Controlling the Order of Concatenation
A critical point to remember is that the COPY *.log command does not guarantee a specific order. It usually processes files alphabetically, but this is not a behavior you should rely on for critical, ordered data.
Solution: Use the + Syntax
If the order of the files in the final combined document is important, you must list them explicitly.
@ECHO OFF
REM This guarantees the files are combined in the correct chronological order.
COPY "2023-10-25.log" + "2023-10-26.log" + "2023-10-27.log" "weekly_report.log"
Common Pitfalls and How to Solve Them
Problem: The Destination File is Included in the Source
This is the most common mistake when concatenating files. Consider the following command:
COPY *.txt all.txt
If a file named all.txt already exists, COPY will read it as a source file. It then tries to overwrite all.txt as the destination, leading to an error and corrupted data.
Content of destination file will be lost.
The process cannot access the file because it is being used by another process.
Solution: Use a Different Name or Location
- Use a different extension: Name the output file something that won't be matched by the wildcard.
COPY *.txt "combined_output.tmp"
REN "combined_output.tmp" "all.txt" - Place the destination in another directory: This is often the cleanest solution.
MKDIR ..\Output 2>NUL
COPY *.txt ..\Output\all.txt
Problem: Handling Files with Headers
When you combine multiple CSV files, you end up with the header row from each file repeated throughout your combined data.
Solution: A More Advanced Script
You need a script that treats the first file differently from the rest. The MORE +1 command can be used to skip the first line of a file. See the practical example below for the full solution.
Practical Example: Consolidating Daily CSV Reports
This script combines multiple daily sales reports into a single monthly report, correctly handling the header row.
Date,Product,Amount
2023-10-25,Widget,150
...
@ECHO OFF
SETLOCAL
SET "OUTPUT_FILE=Monthly_Report.csv"
SET "HEADER_WRITTEN="
ECHO Consolidating CSV files...
IF EXIST "%OUTPUT_FILE%" DEL "%OUTPUT_FILE%"
REM Loop through all sales CSVs.
FOR %%F IN (sales_*.csv) DO (
IF NOT DEFINED HEADER_WRITTEN (
REM For the first file, copy it completely (including header).
COPY "%%F" "%OUTPUT_FILE%" > NUL
SET "HEADER_WRITTEN=true"
) ELSE (
REM For all subsequent files, skip the first line (the header)
REM and append the rest to the output file.
MORE +1 "%%F" >> "%OUTPUT_FILE%"
)
)
ECHO Consolidation complete: %OUTPUT_FILE%
ENDLOCAL
This script intelligently builds a single, clean CSV file with one header and the data from all the daily files.
Conclusion
Combining text files is a simple but powerful feature in Windows Batch, and the COPY command is the best tool for the job.
- For quickly merging files where order doesn't matter, use the
COPY *.ext "output.file"wildcard syntax. - When the merge order is critical, explicitly list the files using the
COPY "file1" + "file2" "output.file"syntax. - Never let your destination file be a part of your source selection. Always use a different name, extension, or location for the output file.
- For complex tasks like merging CSVs with headers, a
FORloop with conditional logic provides the necessary flexibility.