337

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

How can I achieve that?

4

11 回答 11

488

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.

于 2008-09-13T01:53:10.320 回答
92

You can use copy /b like this:

copy /b file1+file2 destfile
于 2008-09-13T01:29:36.773 回答
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.

于 2008-09-13T03:45:06.497 回答
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
于 2008-09-13T02:00:03.847 回答
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

于 2008-09-13T01:32:55.750 回答
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.

于 2016-05-17T20:15:34.943 回答
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
于 2014-05-30T03:49:44.587 回答
0

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.

于 2014-10-15T13:40:05.037 回答
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!

于 2018-11-10T05:17:08.450 回答
0

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

于 2019-01-13T10:03:16.413 回答
-1

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

于 2020-05-13T14:44:25.203 回答