tags:

views:

411

answers:

2

What's the best way of concatenating binary files using Powershell? I'd prefer a one-liner that simple to remember and fast to execute.

The best I've come up with is:

gc -Encoding Byte -Path ".\File1.bin",".\File2.bin" | sc -Encoding Byte new.bin

This seems to work ok, but is terribly slow with large files.

+2  A: 

It's not Powershell, but if you have Powershell you also have the command prompt:

copy /b 1.bin+2.bin 3.bin

As Keith Hill pointed out, if you really need to run it from inside Powershell, you can use:

cmd /c copy /b 1.bin+2.bin 3.bin
João Angelo
copy is an intrinsic command in cmd.exe. You would have to execute cmd /c copy /b 1.bin+2.bin 3.bin
Keith Hill
Nice simple solution, works on any windows computer.Upvoted but accept to Keith since I asked for PS version. Thx
FkYkko
+6  A: 

The approach you're taking is the way I would do it in PowerShell. However you should use the -ReadCount parameter to improve perf. You can also take advantage of positional parameters to shorten this even further:

gc File1.bin,File2.bin -Enc Byte -Read 512 | sc new.bin -Enc Byte

Regarding the use of the -ReadCount parameter, I did a blog post on this a while ago that folks might find useful - Optimizing Performance of Get Content for Large Files.

Keith Hill
I just ran this on my example files and the command went from taking 9 minutes to 3 seconds with the inclusion of the -read param. This is on a x25m drive. Nice. You get my accept.
FkYkko