How to join two binary files on Windows
Asked Answered
H

3

17

I created two binary files. I would like to concatenate both of them into one with the second one starting at offset firstFile.Size in the resulting file. I tried using a command in cygwin on Windows.

I entered the following command in cmd

cat file1.bin file2.bin > file3.bin

It generates an output file but it is 0 bytes in size. Does anyone know how this is done?

Hibiscus answered 13/11, 2018 at 11:11 Comment(5)
Isn't cat for concatenating text files ?Unimproved
If you're using Windows 10 and have WSL installed with a Linux distribution, you can run the command you wrote above and it will workBohemia
@Bohemia why doesn't it work in cygwin then? I thought it was equivalent to running it in WSL.Hibiscus
True, I'd also expect it to work. Maybe it's blocking at the file access level. Does echo "foo" > bar.txt work?Bohemia
@Unimproved For UNIX there is no difference between text files and binary files regarding cat (UNIX does not have an "EOF character" like CP/M and MS-DOS had). So the command should have worked, or some error should have been output (or logged in syslog at least).Vaenfila
I
44

I didn't initially notice that the question was for Cygwin, Here is a solution for DOS anyway (not Cygwin).

Open a command prompt and type COPY /?

COPY lets you concatenate files by using the + operator

It also lets you designate them as binary by using the /B operator

So if you change to the directory with CD MyDir and run the following I would expect your concatentated file to be created

 COPY /B File1.bin + File2.bin file3.bin
Invitation answered 13/11, 2018 at 11:47 Comment(8)
This was flagged as low quality, more explanation for the solution would improve the answer.Imperial
This doesn't concatenate the files, it overwrites the second with the first, creating corrupt files. And it seems to be pretty slow. I only tried to copy 200 bytes to about eighty 200 KB files, and it took over 40 secondsMongolian
You're saying this overwrites File2.bin with File1.bin? What happens to file3.bin? Feel free to post a better answerInvitation
What I always found confusing with /B was: Is it a global option, or is it position-dependent, possibly affecting only the next file to process? Are spaces around + harmful?Vaenfila
/B is mandatory here. Appending will stop on some bytes if this key is omitted. Spaces are OKBriseno
Is there a limit to the size of the file that this will work for? I have two 2.5GB files I want to concatenate into one large file.Dulosis
Try it and see. It's possible that these old DOS tools can't handle it, in which case I suggest Powershell instead. https://mcmap.net/q/394381/-fast-and-simple-binary-concatenate-files-in-powershellInvitation
@U.Windl the /B is in fact position dependent. From ss64.com/nt/copy.html The /A and /B options can appear in multiple locations, with different meanings depending on location. Before any source - they will set the default mode for all source and destination files. After a source - they will set the mode for that source. After the destination - they will set the mode for the destination.Isolating
G
8

To join two (or more) binary files together, the syntax is:

copy file1/b+file2/b file3/b

I use an a DOS or CMD window in an old XP machine to join two 100KB files together, and its almost instant.

Goldofpleasure answered 18/12, 2020 at 19:59 Comment(0)
L
1

In windows last version I have used HXD binary editor. In the file tools manu you have the option of concatenating bin files. Add them and then type the name of the resulting file. Execution is instantaneous.

Laurenalaurence answered 26/8, 2021 at 13:56 Comment(1)
"Execution is instantaneous" is probably relative: When copying a few hundred kB, probably all will go from a read-cache to a write-back cache, delaying the writes after being finished. However when you concatenate multiple parts of a (say 10GB) ISO image, the result cannot be instantaneous, whatever program you use (maybe except you have 64GB RAM).Vaenfila

© 2022 - 2024 — McMap. All rights reserved.