347

I need to join two binary files with a *.bat script on Windows.

How can I achieve that?

Demurgos
  • 1,568
  • 18
  • 40
Artem Tikhomirov
  • 21,497
  • 10
  • 48
  • 68
  • 20
    Your title question and your description question are not totally in sync. type would be the best answer for the title question and copy /b would be the best answer for the description question. You might want to change that title if possible. – Joseph Sep 13 '08 at 06:42
  • 2
    @Joseph `type` can replace only part of `cat`'s functionality (due to distinction between binary and text files on Windows). – jfs Sep 13 '08 at 22:56
  • Cross-duplicate: https://superuser.com/q/434870/52365 – GSerg Jul 08 '19 at 20:53
  • Note that `cat` does more than just concatenate files; another function it performs is copy stdin to stdout (when called with no arguments). See this question for how to achieve that on Windows: https://stackoverflow.com/q/52330841 – Simon Kissane Apr 19 '21 at 11:30

11 Answers11

502

Windows type command works similarly to UNIX cat.

Example 1:

type file1 file2 > file3

is equivalent of:

cat file1 file2 > file3

Example 2:

type  *.vcf > all_in_one.vcf  

This command will merge all the vcards into one.

Greg Dubicki
  • 5,983
  • 3
  • 55
  • 68
Nathan Jones
  • 5,854
  • 1
  • 18
  • 7
  • 1
    The problem with type in this situation is it adds unwanted file headers when typing more than one file specified on the command line. – Greg Hewgill Sep 13 '08 at 01:56
  • 2
    When specifying more than one file on the command line, type outputs a few blank lines and the name of the file, before copying the contents of the file. Try it :) – Greg Hewgill Sep 13 '08 at 02:13
  • 4
    Ah, I hadn't noticed that the headers go to stderr. Good to know. – Greg Hewgill Sep 13 '08 at 03:01
  • 1
    note that type with the arrow (as above) doesn't work with large files (like > 2 GB or 4), at least with XP. You can use the "copy" command, though (see other answer). – rogerdpack Nov 27 '10 at 23:24
  • Note: Echo supports alternate data streams. Type does not. (https://en.wikipedia.org/wiki/NTFS#Alternate_data_streams_.28ADS.29) – WHol Sep 11 '15 at 15:50
  • 3
    Another tip: to avoid showing any other output `type file1 file2 > file3 2>NUL` – Mike T Mar 28 '18 at 21:08
  • THANK YOU! I just spent hours troubleshooting this for Windows. The "type" = "cat" command is the answer. Thank you for sharing it. :) – rcfmonarch Dec 14 '18 at 13:44
  • 1
    `type` (PowerShell's one) converted my text files into UTF-16. Any way to prevent that? – John Dvorak May 10 '19 at 12:48
92

You can use copy /b like this:

copy /b file1+file2 destfile
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 5
    for sequence of files you can use wildcards like "copy /b file* destfile" – Stalinko Feb 12 '14 at 11:21
  • 1
    strange to me that this is not the accepted answer, given the apparent issues with `type` outlined in the comments on the accepted answer... unless this solution has similar drawbacks but is not popular enough for people to note 'em! – Jony Thrive Jun 19 '19 at 19:00
  • Thank you! Works for me. For some reason `type` repeats the content twice in the output file. – kohane15 Nov 14 '20 at 00:47
24

If you have control over the machine where you're doing your work, I highly recommend installing GnuWin32. Just "Download All" and let the wget program retrieve all the packages. You will then have access to cat, grep, find, gzip, tar, less, and hundreds of others.

GnuWin32 is one of the first things I install on a new Windows box.

David Citron
  • 43,219
  • 21
  • 62
  • 72
  • I would recommend GetGnuWin32 (to simplify installation) – jfs Sep 13 '08 at 22:59
  • 3
    If using an external utility is acceptable I'd prefer busybox for Windows which is a single ~600 kB exe incorporating ~30 Unix utilities. The only difference is that one should use "busybox cat" command instead of simple "cat" – Fr0sT Jan 10 '14 at 13:59
14

Shameless PowerShell plug (because I think the learning curve is a pain, so teaching something at any opportunity can help)

Get-Content file1,file2

Note that type is an alias for Get-Content, so if you like it better, you can write:

type file1,file2
Jay Bazuzi
  • 45,157
  • 15
  • 111
  • 168
  • 1
    It works, but it's dog slow. I found this question while sitting waiting for powershell to concatenate some large video files which was taking half an hour per file. I killed the process and tried it with cmd using `type file1 file2 > dest` and each one was done in seconds. – stib Jan 23 '15 at 01:59
  • 1
    beware, [powershell may corrupt your binary files e.g., `'\n'` may be replaced with `'\r\n'`](http://stackoverflow.com/a/33959798/4279) – jfs Feb 02 '16 at 09:03
  • It converted my files into UTF-16, for some reason. `-Encoding UTF8` didn't change anything. – John Dvorak May 10 '19 at 12:57
  • 1
    @stib because Get-Content works with texts and you need `-AsByteStream` to tell it you don't want it to split into lines. In older PowerShell you'll need `-Raw` or `-Encoding Byte` – phuclv Jul 24 '22 at 03:19
  • @JohnDvorak Get-Content returns lines of text by default, which is always UTF-16 in .NET framework. See the above comment to get binary files – phuclv Jul 24 '22 at 03:22
7

Just use the dos copy command with multiple source files and one destination file.

copy file1+file2 appendedfile

You might need the /B option for binary files

VFDan
  • 831
  • 10
  • 26
simon
  • 5,777
  • 7
  • 30
  • 36
2

In Windows 10's Redstone 1 release, the Windows added a real Linux subsystem for the NTOS kernel. I think originally it was intended to support Android apps, and maybe docker type scenarios. Microsoft partnered with Canonical and added an actual native bash shell. Also, you can use the apt package manager to get many Ubuntu packages. For example, you can do apt-get gcc to install the GCC tool chain as you would on a Linux box.

If such a thing existed while I was in university, I think I could have done most of my Unix programming assignments in the native Windows bash shell.

sam msft
  • 537
  • 6
  • 17
1

If you simply want to append text to the end of existing file, you can use the >> pipe. ex:

echo new text >>existingFile.txt
James John McGuire 'Jahmic'
  • 11,728
  • 11
  • 67
  • 78
1

So i was looking for a similar solution with the abillity to preserve EOL chars and found out there was no way, so i do what i do best and made my own utillity This is a native cat executable for windows - https://mega.nz/#!6AVgwQhL!qJ1sxx-tLtpBkPIUx__iQDGKAIfmb21GHLFerhNoaWk

Usage: cat file1 file2 file3 file4 -o output.txt
-o | Specifies the next arg is the output, we must use this rather than ">>" to preserve the line endings

I call it sharp-cat as its built with C#, feel free to scan with an antivirus and source code will be made available at request

0

If you have to use a batch script and have python installed here is a polyglot answer in batch and python:

1>2# : ^
'''
@echo off
python "%~nx0" " %~nx1" "%~nx2" "%~nx3"
exit /b
rem ^
'''
import sys
import os

sys.argv = [argv.strip() for argv in sys.argv]
if len(sys.argv) != 4:
    sys.exit(1)

_, file_one, file_two, out_file = sys.argv

for file_name in [file_one, file_two]:
    if not os.path.isfile(file_name):
        print "Can't find: {0}".format(file_name)
        sys.exit(1)

if os.path.isfile(out_file):
    print "Output file exists and will be overwritten"

with open(out_file, "wb") as out:
    with open(file_one, "rb") as f1:
        out.write(f1.read())

    with open(file_two, "rb") as f2:
        out.write(f2.read())

If saved as join.bat usage would be:

join.bat file_one.bin file_two.bin out_file.bin

Thanks too this answer for the inspiration.

YorSubs
  • 3,194
  • 7
  • 37
  • 60
Noelkd
  • 7,686
  • 2
  • 29
  • 43
0

I try to rejoin tar archive which has been splitted in a Linux server.

And I found if I use type in Windows's cmd.exe, it will causes the file being joined in wrong order.(i.e. type sometimes will puts XXXX.ad at first and then XXXX.ac , XXXX.aa etc ...)

So, I found a tool named bat in GitHub https://github.com/sharkdp/bat which has a Windows build, and has better code highlight and the important thing is, it works fine on Windows to rejoin tar archive!

  • that's because cmd commands don't sort files by default and it happens that you used a wildcard and run `type XXXX.*` on a FAT32 partition which stores directory entries in a linear list and when the files aren't created in the correct order then it'll mess up your things, unlike NTFS which stores in a B-tree and files will be sorted in English locale automatically. Use NTFS and PowerShell instead – phuclv Jul 24 '22 at 03:27
0

Windows type command has problems, for example with Unicode characters on 512 bytes boundary. Try Cygwin's cat.

user42391
  • 21
  • 1
  • 1