331
votes

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

How can I achieve that?

11
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
@Joseph type can replace only part of cat's functionality (due to distinction between binary and text files on Windows).jfs
Cross-duplicate: superuser.com/q/434870/52365GSerg
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: stackoverflow.com/q/52330841Simon Kissane

11 Answers

481
votes

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.

91
votes

You can use copy /b like this:

copy /b file1+file2 destfile
24
votes

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.

14
votes

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
7
votes

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

2
votes

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.

1
votes

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

echo new text >>existingFile.txt
0
votes

If you have to use a batch script and have python installed here is a polygot 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.

0
votes

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!

0
votes

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

-1
votes

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