28-11-2020  (736 ) Categoria: Articles

Merge concatenate files in windows

MS DOS copying several files to one file

Asked 9 years, 4 months ago
Viewed 117k times
19

I am trying to take a folder that has several .csv files in it and combine all of these files and the information in them, into one file using MS DOS. Any suggestions?

7 Answers

41
copy *.csv new.csv

No need for /b as csv isn't a binary file type.

20
copy /b file1 + file2 + file3 newfile

Each source file must be added to the copy command with a +, and the last filename listed will be where the concatenated data is copied to.

  • 1
    there isnt a copy all (*) command? BC i have like 30 to 40 files. – edmon Jul 20 '11 at 15:45
  • This version lets you specify the exact order in which you'd like the files copied. with the *.csv method above, you'll get the files in random-ish order. – Marc B Jul 20 '11 at 18:19
5

If this is part of a batch script (.bat file) and you have a large list of files, you can use a multi-line ^, and optional /Y flag to suppresses prompting to confirm you want to overwrite an existing destination file.

REM Concatenate several files to one
COPY /Y ^
    this_is_file_1.csv + ^
    this_is_file_2.csv + ^
    this_is_file_3.csv + ^
    this_is_file_4.csv + ^
    this_is_file_5.csv + ^
    this_is_file_6.csv + ^
    this_is_file_7.csv + ^
    this_is_file_8.csv + ^
    this_is_file_9.csv ^
        output_file.csv

This is tidier than performing the command on one line.

  • This format is much cleaner. And I can easily format and prepare thousands of file names in VS Code. Thanks! – jinhr Aug 13 '19 at 14:59
  • this doesn't work in DOS (as this question is tagged) because DOS doesn't support ^ as an escape character – phuclv Dec 14 '19 at 10:30
4

filenames must sort correctly to combine correctly!

file1.bin file2.bin ... file10.bin wont work properly

file01.bin file02.bin ... file10.bin will work properly

c:>for %i in (file*.bin) do type %i >> onebinary.bin

Works for ascii or binary files.

1
for %f in (filenamewildcard0, filenamewildcard1, ...) do echo %f >> newtargetfilename_with_path

Same idea as Mike T; might work better under MessyDog's 127 character command line limit

1
type data1.csv > combined.csv
type data2.csv >> combined.csv
type data3.csv >> combined.csv
type data4.csv >> combined.csv

etc.

Assume that your using files without headers and all files have the same columns.

Windows batch - concatenate multiple text files into one

Asked 5 years, 6 months ago
Viewed 151k times
49

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.

9 Answers

68

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
  • 5
    this is the most straightforward equivalent of the unix command, this should be the accepted answer – Asped Mar 4 '19 at 15:00
  • 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
  • 4
    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 at 0:27
37

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

copy file1.txt + file2.txt + file3.txt concattedfile.txt
  • 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. – UuDdLrLrSs Nov 9 '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 9 '17 at 14:42
  • thank you for testing that out. I will have to experiment further to try and find the reason! – UuDdLrLrSs Nov 9 '17 at 19:15
  • 9
    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
10

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

  • 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 5 '17 at 23:30
  • don't forget to remove the extra caracter 0x1A at the end of the file – Mimouni Sep 27 at 10:55
6

In Win 7, on the command prompt use:

copy *.txt combined.txt
5

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
3

We can use normal CAT command to merge files..

D:> cat *.csv > outputs.csv

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

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.

1

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.




versió per imprimir