86

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.

12
  • 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, 2015 at 18:47
  • Its windows XP so there is no powershell
    – SpeedEX505
    May 26, 2015 at 18:48
  • You are going to need a lovely FOR command to do this: stackoverflow.com/questions/9234207/…. 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. May 26, 2015 at 18:48
  • 1
    You can install Powershell 2.0 on Windows XP. See support.microsoft.com/en-us/kb/968929 May 26, 2015 at 18:50
  • 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, 2015 at 6:52

10 Answers 10

135

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
4
  • 9
    this is the most straightforward equivalent of the unix command, this should be the accepted answer
    – Asped
    Mar 4, 2019 at 15:00
  • 2
    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, 2019 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, 2020 at 0:27
  • works pretty fine, thank you. NOTE: Much slower than in LINUX... May 16, 2021 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
6
  • 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. Nov 9, 2017 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 9, 2017 at 14:42
  • thank you for testing that out. I will have to experiment further to try and find the reason! Nov 9, 2017 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, 2018 at 14:16
  • 1
    this is much faster on large files, than the accepted answer
    – ashleedawg
    Jul 31, 2021 at 15:42
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.

1
  • 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 1 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

2
  • 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. Jun 5, 2017 at 23:30
  • don't forget to remove the extra caracter 0x1A at the end of the file
    – Mimouni
    Sep 27, 2020 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
5

We can use normal CAT command to merge files..

D:> cat *.csv > outputs.csv

3
  • 8
    There is no CAT on Windows.
    – Yarik
    Jun 6, 2017 at 0:19
  • 3
    Some packages like Git come with cat, maybe he mistook it for a DOS command. Nov 9, 2017 at 13:15
  • 1
    for sure microsoft released cat.exe -> its included in the nt4 resource kit - see news.microsoft.com/1996/10/29/…
    – Bernhard
    Jul 12, 2018 at 14:19
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.

2
  • OP edited their post: Its Windows XP. Is there also a solution for Win XP?
    – CodeF0x
    Jul 2, 2018 at 16:05
  • use resource kit nt 4.0 - there is an cat.exe (still working on win10)
    – Bernhard
    Jul 12, 2018 at 14:20
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
1
  • 1
    The result from your answer depends on system's local setting.
    – Endoro
    May 26, 2015 at 19:31
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.

-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).

1
  • 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 at 21:38

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.