85

I need to create a script, which concatenates multiple text files into one. I know it's simple to use

type *.txt > merged.txt

But the requirement is to "concatenate files from same day into file day_YYYY-DD-MM.txt" I am a Linux user and Windows batch is hell for me. It's Windows XP.

Null
  • 1,950
  • 9
  • 30
  • 33
SpeedEX505
  • 1,976
  • 4
  • 22
  • 33
  • You should tell us if it needs to be done in a "real" batch file or if powershell / Windows Scripting host is available. For starters you should mention which Windows version needs to be supported (minimum / maximum) – Marged May 26 '15 at 18:47
  • Its windows XP so there is no powershell – SpeedEX505 May 26 '15 at 18:48
  • You are going to need a lovely FOR command to do this: http://stackoverflow.com/questions/9234207/search-by-date-using-command-line. Personally, I would not mess with things like this in batch. Even if you can get it to work, the types of object you can use are limited--Powershell is the way to go. – Tony Hinkle May 26 '15 at 18:48
  • 1
    You can install Powershell 2.0 on Windows XP. See https://support.microsoft.com/en-us/kb/968929 – Tony Hinkle May 26 '15 at 18:50
  • Is this your home work? – Endoro May 26 '15 at 18:50
  • To stress this again: is Windows Scripting Host available ? You can find out by calling "cscript" on the command line. If WSH is installed there will be output on your screen, if not you will receive a "command not found" – Marged May 26 '15 at 18:52
  • @Endoro No its not homework – SpeedEX505 May 26 '15 at 18:59
  • @SpeedEX505 As WSH is installed you can use JavaScript to search for files. Perhaps this a pointer for a Google search if you don't get a readymade answer here. Unfortunately I don't have such an example for you ... – Marged May 26 '15 at 19:40
  • I wrote it in bash and run it with cygwin. Its much simpler – SpeedEX505 May 26 '15 at 19:52
  • 1
    I did not suggest cygwin because I thought you want a minimal fuzz solution. The title of your question suggests you want a solution based on Windows mechanisms. – Marged May 27 '15 at 06:52
  • I am also Linux user to your comment was very funny to me as I can relate :-) I guess to be fair we should be using powershell now. – Mike Q May 11 '20 at 13:54
  • When I concated using the above command, Windows began concatenating the resulting `merge.txt` file into itself and I had to exit the process. The file got ridiculously big. – Altimus Prime Mar 19 '21 at 04:00

10 Answers10

134

Windows type command works similarly to UNIX cat.

Example 1: Merge with file names (This will merge file1.csv & file2.csv to create concat.csv)

type file1.csv file2.csv > concat.csv

Example 2: Merge files with pattern (This will merge all files with csv extension and create concat.csv)

When using asterisk(*) to concatenate all files. Please DON'T use same extension for target file(Eg. .csv). There should be some difference in pattern else target file will also be considered in concatenation

type  *.csv > concat_csv.txt
Shantanu Sharma
  • 3,661
  • 1
  • 18
  • 39
  • 9
    this is the most straightforward equivalent of the unix command, this should be the accepted answer – Asped Mar 04 '19 at 15:00
  • 1
    It is also the most inefficient way because "type" will do its job in like 4 KiB chunks, making the operation incredibly slow. – Ivan Voras Dec 20 '19 at 19:50
  • 14
    DON'T USE THE SAME EXTENSION FOR THE TARGET FILE. I just waited a few minutes when I ran this command until I noticed it got stuck in an infinite loop adding the target file to it-self and creating an ever-growing file (reached 15GB when I noticed it's stuck when trying to combine 2MB of files). – Adam Tal Jan 14 '20 at 00:27
  • works pretty fine, thank you. NOTE: Much slower than in LINUX... – Kerim Yagmurcu May 16 '21 at 15:39
48

At its most basic, concatenating files from a batch file is done with 'copy'.

copy file1.txt + file2.txt + file3.txt concattedfile.txt
Lance
  • 3,824
  • 4
  • 21
  • 29
  • 1
    I tried copy *.xyz all.bin but the resulting file all.bin only contained a subset of the xyz files in that directory. I can't see why it didn't include all of them. Is the plus syntax necessary? If so that is inconvenient for a large number of files. – StayOnTarget Nov 09 '17 at 13:16
  • 1
    I just created an example per your specs and it behaved as expected. I created "1.xyz", "2.xyz", and "3.xyz". Then ran "copy *.xyz all.bin" and the resulting "all.bin" contained the contents of all 3 files. So I don't know why it's not working for you. – Lance Nov 09 '17 at 14:42
  • thank you for testing that out. I will have to experiment further to try and find the reason! – StayOnTarget Nov 09 '17 at 19:15
  • 13
    You should also mention the "/b" (binary) operator. Using it with text files also copies the BOM header (eg. UTF8+BOM, UCS2+BOM) of each file together and produces massive complications later on... – Bernhard Jul 12 '18 at 14:16
  • 1
    this is much faster on large files, than the accepted answer – ashleedawg Jul 31 '21 at 15:42
  • I've used this for literally decades in DOS cmd shell. But it doesn't work in Power Shell. Use Windows 10's cmd. – Doug Null Feb 08 '22 at 17:48
15

In Win 7, navigate to the directory where your text files are. On the command prompt use:

copy *.txt combined.txt

Where combined.txt is the name of the newly created text file.

Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125
  • Win 11: for me this is equivalent to "copy file1.txt combined.txt", then "copy file2.txt combined.txt" etc. That is, combined.txt is a copy of the last file of the list, not a concatenation of them all. Type ... worked. – Convexity Feb 01 '23 at 10:06
13

Place all files need to copied in a separate folder, for ease place them in c drive.

Open Command Prompt - windows>type cmd>select command prompt.

You can see the default directory pointing - Ex : C:[Folder_Name]>. Change the directory to point to the folder which you have placed files to be copied, using ' cd [Folder_Name] ' command.

After pointing to directory - type 'dir' which shows all the files present in folder, just to make sure everything at place.

Now type : 'copy *.txt [newfile_name].txt' and press enter.

Done!

All the text in individual files will be copied to [newfile_name].txt

Kingsman
  • 174
  • 1
  • 10
  • 5
    This appears to append an extra 0x1A character to the end of the resulting file, which if I recall from the way-back-bad-old-days was "control Z", an end of file mark. – ALEXintlsos Jun 05 '17 at 23:30
  • don't forget to remove the extra caracter 0x1A at the end of the file – Mimouni Sep 27 '20 at 10:55
8

I am reiterating some of the other points already made, but including a 3rd example that helps when you have files across folders that you want to concatenate.

Example 1 (files in the same folder):

copy file1.txt+file2.txt+file3.txt file123.txt

Example 2 (files in same folder):

type *.txt > combined.txt

Example 3 (files exist across multiple folders, assumes newfileoutput.txt doesn't exist):

for /D %f in (folderName) DO type %f/filename.txt >> .\newfileoutput.txt
Tobias Wilfert
  • 919
  • 3
  • 14
  • 26
Gigasoup
  • 81
  • 1
  • 1
5

We can use normal CAT command to merge files..

D:> cat *.csv > outputs.csv

Sadheesh
  • 895
  • 9
  • 6
2

cat "input files" > "output files"

This works in PowerShell, which is the Windows preferred shell in current Windows versions, therefore it works. It is also the only version of the answers above to work with large files, where 'type' or 'copy' fails.

Stephen
  • 29
  • 1
1

Try this:

@echo off
set yyyy=%date:~6,4%
set mm=%date:~3,2%
set dd=%date:~0,2%

set /p temp= "Enter the name of text file: "
FOR /F "tokens=* delims=" %%x in (texto1.txt, texto2.txt, texto3.txt) DO echo %%x >> day_%temp%.txt

This code ask you to set the name of the file after "day_" where you can input the date. If you want to name your file like the actual date you can do this:

FOR /F "tokens=* delims=" %%x in (texto1.txt, texto2.txt, texto3.txt) DO echo %%x >> day_%yyyy%-%mm%-%dd%.txt
Fernando Madriaga
  • 438
  • 1
  • 6
  • 17
0

You can do it using type:

type"C:\<Directory containing files>\*.txt"> merged.txt

all the files in the directory will be appendeded to the file merged.txt.

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
-1

copy is definitely much faster than type - but it sometimes (with large files?) adds a SUB character at the end of the file. So, strictly speaking, it does not simply concatenate the files in the same way as cat in Unix.

So, the correct answer is to use cat - either in something like Git Bash (where it has the same syntax as in Unix), or PowerShell (where it does not).

  • The SUB is an artefact of CP/M, where it was used as an EOF marker (CP/M "length" was blocks, not bytes). MSDOS was designed to be able to read CP/M files, & since it was likely at the time that CP/M would be used to read the resuts from MSDOS, the ability to control the end-of-file marker was included in `copy` using the `/A` and `/B` switches. By default it applies `/A` (`^Z`=EOF) to input files, but if the `/B` flag appears before the input filenames, then the `^Z` is preserved. Each individual file can be controlled by its own switch. If the destination filename has `/A`, SUB is appended – Magoo Feb 11 '23 at 21:38