4 May 2016

Batch Program to Merge Multiple Text (.txt) Format Files

Leave a Comment
In my previous article, I had discussed on how to merge multiple files using Command Prompt. You can also use that tutorial to hide your private files by merging them with another format file (basically with rar format). Instead of doing that long programming, you can create a simple batch program to merge your text files.

Merge Text Files With Batch Program

Once created, you can use it any number of time. Moreover, this batch program is a file so you can also share it with your friends or other colleagues. In this short tutorial, I will guide you how to create this program.

Creating a batch program

Before moving to the steps, first let me introduce you with batch programming. Batch is a scripting language which works in Windows, DOS and OS 2 platform and can be programmed in Notepad. It consist of simple commands to be executed by command line interpreter. The basic file extension of batch program is ".bat". Now to create it, follow these simple steps -
  1. First of all, open Notepad. (Search it in start menu)
  2. Now copy paste below code inside it.
    @echo off
    title Merge Text Files
    color 9f
    echo Welcome to the program!
    set /p loc=Please enter the location of folder where all txt files are located:
    cd %loc%
    set /p name=Type the name of new merged file (without adding ".txt"):
    echo Press enter button to continue!
    copy /b *.txt %name%.txt
    echo Done!
    echo Press any key to exit!
  3. To save it, click on "File > Save As" option.
  4. Now choose the location where you want to save this program (say I choose Desktop) and type its file name. (e.g., "batchprogram.bat")

    Make sure to add ".bat" at the end of file name. It is the format of batch program and without it, your program will not work.
  5. Finally hit save button and that's it.
Now whenever you want to merge your text files, simply copy them and paste them in new folder. After that, move that folder to Desktop and open this batch program.

It will ask you type the location of folder. This location should match with the location of folder where you've saved all those txt files. For example, If I had saved them on Desktop under "Vivek" named folder then I will type this -


Here username is your account name. You can find it by executing "echo %username%" code in new Command Prompt window. You can also specific the folder location manually by changing the above code.

Next to this, it will ask you to type the name of new merged file. Just give it any name (e.g., "final-file"). Don't add file format at the end as I had already added it in the code. Finally press ENTER button and it will automatically merge all your txt files within second.

Merge Text Files Preview

To check the merged data, go back to the folder and open the merge file. Merging txt files will combine their text. For example, If you've two text files having "I love you" and "but as a friend" words then the new merged text will be "I love you but as a friend". This order can be opposite if the file containing "but as a friend" will come first. Then the new text will be "but as a friend I love you" (which is meaningless).

Note: This batch program will scan your files numerically/alphabetically. You should arrange the files in the order of data that you wants after merging. This can be possible by renaming. For example, If you want to move any file at the top then, rename it to "1.txt" and rename other files to 2,3,4 and so on. This will automatically arrange them in your desired order.

Actually this program is just for fun and it has very less practical use (speaking frankly, I had never used it before) but you can use it if you have bunch of text files and want them in an organized way.

Also read: How to create calculator with batch program

Explanation to code

This part is for advanced users who are interested in batch programming and wants to know about how this program works. This is very simple (as simple as creating a new Hello World in C++. Yeah!). There are 3 most important commands in this program which are set, cd and copy.

cd command is used to move the Command Prompt to a particular location. The basic syntax is "cd [address here]". e.g., "cd C:\Users\Username\Desktop"

On the other hand, copy command is used to copy one or more files. Here it will merge all txt files and then copy their data in new merged file. In above code, I had also used an asterisk (*) wildcard. Asterisk is used to select all items, no matter what their name is. You can find more about this copy command by executing "copy /?" code.

Another important command is set. This is used to take input from user and then save it in variable. For example, "set /p data=type anything" code will declare a new variable named as "data" which will be assigned a value that user will type. If you want to print this variable, simply put it under percent marks like "%data%". (Same thing I had used in above program)

There are few more basic commands which are explained as follows -

  • echo - echo is used to print text
  • color 9f - color command will change font and background color.
  • title - sets the title for the Command Prompt windows.
  • pause>nul - This will pause the screen until user press any key.
  • exit - This will quit/exit the current window.
Now I just hope that you've complete knowledge about this batch program and you can easily merge your text files. If you're facing any problem, feel free to ask me in comment box.

Leave A Comment