Question

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

How can I achieve that?

Was it helpful?

Solution

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.

OTHER TIPS

You can use copy /b like this:

copy /b file1+file2 destfile

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.

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

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

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.

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

echo new text >>existingFile.txt

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.

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!

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top