In this blog, we will go through the procedure to concatenate various text files into a new file in Windows. The content of this blog is as follows:
- Using Command Prompt
- Using PowerShell
Method 1: Using Command Prompt to Concatenate Multiple Text Files in Windows
Files are the main components of the operating system used to store data. Sometimes users wish to concatenate two or more files into a single file.
The command prompt can be used via the following commands to concatenate multiple text files in windows:
- Through type command
- Through copy command
Let’s dig into the above commands one by one.
We will concatenate two text files “File1.txt” and “File2.txt”. The content of the files is shown below:
File1.txt:
File2.txt:
Let’s concatenate the above-displayed text files using the following commands.
Utilize “type” Command to Concatenate Various Text Files
The “type” command is a Windows built-in command line utility used in PowerShell and Command Prompt. The following steps guide you to use “type” command to concatenate files:
Step 1: Run Windows Command Prompt
First press “CTRL+R” to execute the “Run” box. Then make a search for “cmd” and hit the “OK” button to open Command Prompt:
Step 2: Merge Multiple Files
Let’s try to merge two files “File1” and “File2” into the third file “New”. For this purpose, utilize the below-provided command. The “type” command will concatenate File1 and File2 into a new file “New”:
Step 3: Verify File Concatenation
Type command can also be used to view file content. To check whether the file is concatenated use the “type” command and the name of the file in which the other file is concatenated:
The “type” command can also be used to concatenate all the text files of the current directory. The example command is provided below:
Note: The “type” command can be used in the PowerShell as well.
Utilize the “copy” Command to Concatenate Various Text Files
The “copy” command is also used to merge the content of different files into one single file. As with the “copy” name of the command, this command is used to copy the content:
You can see that we have copied the “File1” and “File2” into the new file “Sample”.
But we cannot utilize copy command to view the content of files because copy command is only used for copying the content. To view file content, utilize the “type” command:
Let’s check out how we can concatenate multiple text files into one file using PowerShell.
Method 2: Using PowerShell to Concatenate Multiple Text Files in Windows
File concatenation can also be done using Windows PowerShell. Following are the commands that are effectively used to merge multiple files into one single file:
- Using Get-content command
- Using cat command
- Using gc command
Let’s go through all the above-mentioned commands one by one to concatenate various text files.
Utilize “Get-content” Command to Concatenate Various Text Files
The “Get-content” command is utilized to access the content of files. Here, we will use the “Get-content” command to get the content of multiple files and save accessed content into a new text file. Follow the provided steps to check out how we can concatenate multiple files using the Get-content command.
Step 1: Open Windows PowerShell
First, open the Windows PowerShell by searching “Windows PowerShell” in the “Startup” menu:
Step 2: Concatenate Multiple Files
Utilize the below-mentioned command to concatenate two text files into one single file. The “Get-content” command is used to access the content of “File1.txt” and “File2.txt” then pipe “|” operator is used to pipe out accessed content to out-file command and save the file content into a new text file “NewFile.txt”:
Let’s verify the concatenation of files by viewing the “NewFile.txt” content using “Get-content” command:
Utilize the “cat” Command to Concatenate Various Text Files
The word “cat” is derived from concatenation. The “cat” command is primarily used for concatenation purposes. Here, the first command concatenates the “File1.txt” and “File2.txt”. While the second command verifies the concatenation:
> cat Text.txt
Utilize the “gc” Command to Concatenate Various Text Files
Note: Various PowerShell cmdlets have Aliases which serve the same purpose as of the cmdlet. The “gc” keyword is an alias of the cmdlet “Get-Content”. You can use the “gc” keyword to serve the same purpose as of “Get-Content”. The first command concatenates the “File1.txt” and “File2.txt” and saves the content into a “NewFile1.txt”. Whereas the second command shows the content of the file:
> gc NewFile.txt
We have illustrated the commands used to concatenate multiple text files in Windows.
Conclusion
While doing file handling operations, concatenation is a merging of multiple files. To concatenate the various text files into one single file, users can either use Command Prompt commands or Windows PowerShell commands. In the Command Prompt, “type” and “copy” commands are utilized to concatenate multiple files. In Windows PowerShell, the “Get-content”, “gc”, “cat” and “type” commands are used to concatenate multiple files. We have compiled the different commands used to concatenate multiple text files into a new text file in Windows.