Powershell中快速简单的二进制连接文件

FkY*_*kko 24 powershell

使用Powershell连接二进制文件的最佳方法是什么?我更喜欢简单易记和快速执行的单线程.

我提出的最好的是:

gc -Encoding Byte -Path ".\File1.bin",".\File2.bin" | sc -Encoding Byte new.bin
Run Code Online (Sandbox Code Playgroud)

这似乎工作正常,但对于大文件来说速度非常慢.

Kei*_*ill 28

您正在采取的方法是我在PowerShell中执行此操作的方法.但是,您应该使用-ReadCount参数来改善性能.您还可以利用位置参数来进一步缩短这一点:

gc File1.bin,File2.bin -Enc Byte -Read 512 | sc new.bin -Enc Byte
Run Code Online (Sandbox Code Playgroud)

关于-ReadCount参数的使用,我刚才做了一篇博文,人们可能会觉得有用 - 优化大文件获取内容的性能.

  • 我只是在我的示例文件上运行此命令,并且命令从9分钟变为3秒,其中包含-read参数.这是一个x25米的驱动器.尼斯.你得到我的接受. (3认同)

Joã*_*elo 25

它不是Powershell,但是如果你有Powershell,你也有命令提示符:

copy /b 1.bin+2.bin 3.bin
Run Code Online (Sandbox Code Playgroud)

正如Keith Hill所指出的,如果你真的需要从Powershell中运行它,你可以使用:

cmd /c copy /b 1.bin+2.bin 3.bin 
Run Code Online (Sandbox Code Playgroud)

  • copy是cmd.exe中的内部命令.你必须执行cmd/c copy/b 1.bin + 2.bin 3.bin (7认同)
  • 另请注意,`copy`支持通配符.因此`copy/b*.bin out.bin`将连接所有bin文件,输出速度非常快(比使用PowerShell快得多). (4认同)
  • 谢谢...它比接受的答案快大约十亿倍;)。当我尝试从 PowerShell 运行它时,我错过了“cmd /c”。有时,旧方法仍然是最好的。 (2认同)

小智 6

我最近遇到了类似的问题,我想将两个大(2GB)文件附加到一个文件(4GB)中。

我尝试调整 Get-Content 的 -ReadCount 参数,但无法提高大文件的性能。

我采用了以下解决方案:

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"
} 
Run Code Online (Sandbox Code Playgroud)

表现:

  • cmd.exe /c copy file1+file2 File3 约 5 秒(最佳)
  • gc file1,file2 |sc file3 大约1100秒(恶心)
  • join-file File1,File2 File3 约16秒(OK)