大文件复制期间的进度(Copy-Item & Write-Progress?) [英] Progress during large file copy (Copy-Item & Write-Progress?)

查看:72
本文介绍了大文件复制期间的进度(Copy-Item & Write-Progress?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 PowerShell 中复制一个非常大的文件(从一台服务器到另一台服务器)并显示其进度?

Is there any way to copy a really large file (from one server to another) in PowerShell AND display its progress?

有一些解决方案可以将 Write-Progress 与循环结合使用来复制许多文件并显示进度.但是我似乎找不到任何可以显示单个文件进度的东西.

There are solutions out there to use Write-Progress in conjunction with looping to copy many files and display progress. However I can't seem to find anything that would show progress of a single file.

有什么想法吗?

推荐答案

我还没有听说 Copy-Item 的进展.如果您不想使用任何外部工具,您可以尝试使用流.缓冲区大小因人而异,您可以尝试不同的值(从 2kb 到 64kb).

I haven't heard about progress with Copy-Item. If you don't want to use any external tool, you can experiment with streams. The size of buffer varies, you may try different values (from 2kb to 64kb).

function Copy-File {
    param( [string]$from, [string]$to)
    $ffile = [io.file]::OpenRead($from)
    $tofile = [io.file]::OpenWrite($to)
    Write-Progress -Activity "Copying file" -status "$from -> $to" -PercentComplete 0
    try {
        [byte[]]$buff = new-object byte[] 4096
        [long]$total = [int]$count = 0
        do {
            $count = $ffile.Read($buff, 0, $buff.Length)
            $tofile.Write($buff, 0, $count)
            $total += $count
            if ($total % 1mb -eq 0) {
                Write-Progress -Activity "Copying file" -status "$from -> $to" `
                   -PercentComplete ([long]($total * 100 / $ffile.Length))
            }
        } while ($count -gt 0)
    }
    finally {
        $ffile.Dispose()
        $tofile.Dispose()
        Write-Progress -Activity "Copying file" -Status "Ready" -Completed
    }
}

这篇关于大文件复制期间的进度(Copy-Item & Write-Progress?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆