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
I had a similar problem recently, where I wanted to append two large (2GB) files into a single file (4GB).
I tried to adjust the -ReadCount parameter for Get-Content, however I couldn't get it to improve my performance for the large files.
I went with the following solution:
function Join-File (
[parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)]
[string[]] $Path,
[parameter(Position=1,Mandatory=$true)]
[string] $Destination
)
{
write-verbose "Join-File: Open Destination1 $Destination"
$OutFile = [System.IO.File]::Create($Destination)
foreach ( $File in $Path ) {
write-verbose " Join-File: Open Source $File"
$InFile = [System.IO.File]::OpenRead($File)
$InFile.CopyTo($OutFile)
$InFile.Dispose()
}
$OutFile.Dispose()
write-verbose "Join-File: finished"
}
Performance:
cmd.exe /c copy file1+file2 File3 around 5 seconds (Best)gc file1,file2 |sc file3 around 1100 seconds (yuck)join-file File1,File2 File3 around 16 seconds (OK)