15

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?

preciousbetine
  • 2,959
  • 3
  • 13
  • 29
  • Isn't cat for concatenating text files ? – auburg Nov 13 '18 at 11:13
  • 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 work – edwillys Jun 30 '21 at 11:57
  • @edwillys why doesn't it work in cygwin then? I thought it was equivalent to running it in WSL. – preciousbetine Aug 27 '21 at 17:12
  • True, I'd also expect it to work. Maybe it's blocking at the file access level. Does `echo "foo" > bar.txt` work? – edwillys Sep 03 '21 at 13:36
  • @auburg 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). – U. Windl Nov 23 '21 at 10:44

3 Answers3

38

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
Nick.Mc
  • 18,304
  • 6
  • 61
  • 91
  • 1
    This was flagged as low quality, more explanation for the solution would improve the answer. – jcubic Nov 13 '18 at 15:05
  • 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 seconds – bryc Oct 09 '20 at 11:24
  • 3
    You're saying this overwrites File2.bin with File1.bin? What happens to file3.bin? Feel free to post a better answer – Nick.Mc Oct 09 '20 at 11:29
  • 1
    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? – U. Windl Nov 23 '21 at 10:47
7

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.

U. Windl
  • 3,480
  • 26
  • 54
Billy R
  • 71
  • 1
  • 1
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.

  • "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). – U. Windl Nov 23 '21 at 10:52