PowerShell Robo复制文件传输进度

时间:2016-07-07 13:44:55

标签: powershell robocopy

我目前有一个工作的Robo Copy功能,可以跟踪当前正在传输的文件的百分比。我一直试图让它跟踪从总数中传输的文件数量,例如,传输的10/50文件。我已经尝试过Trevor Sullivan的进步方式,但它不适用于我的剧本。

以下是细分:

Function global:music_Transfer{

if($WPFmusic_checkBox.IsChecked -eq $true){

$quotation = ":"  
$source = "$DLetter$quotation\Users\$associd\Music"
$destination = "C:\Users\$env:USERNAME\Music"
$copy = "/COPY:DTO"
$files = "*.*"
$backup = "/B"
$reportextrafiles = "/X"
$verboseoutput = "V"
$fullpathname = "FP"
$mirror = "/MIR"
$backupmode = "/ZB"
$subfolders = "/E"
$Retries = "/R:2"
$consolewrite = "/TEE"

robocopy.exe $source $destination $files $backup $reportextrafiles $verboseoutput $fullpathname $mirror $backupmode $subfolders $Retries $consolewrite | ForEach-Object -Process {
    $data = $_.Split([char]9);
    if (($data.Count -gt 4) -and ("$($data[4])" -ne ""))
    {
        $file = "$($data[4])"
        Write-Progress "Percentage $($data[0])" -Activity "Music" -CurrentOperation "$($file)" -ErrorAction SilentlyContinue; 
    }
    else
    {
        Write-Progress "Percentage $($data[0])" -Activity "Music" -CurrentOperation "$($file)"
    }
}
# Robocopy has a bitmask set of exit codes, so only complain about failures:
[int] $exitCode = $global:LastExitCode;
[int] $someCopyErrors = $exitCode -band 8;
[int] $seriousError = $exitCode -band 16;
if (($someCopyErrors -ne 0) -or ($seriousError -ne 0))
{
    Write-Error "ERROR: robocopy failed with a non-successful exit code: $exitCode"    
}
}
}

它跟踪正在传输的内容和单个文件的进度,但不跟踪总文件。任何帮助,将不胜感激。感谢。

1 个答案:

答案 0 :(得分:1)

没有经过测试,但这些内容可能会有效

Function global:music_Transfer{

if($WPFmusic_checkBox.IsChecked -eq $true){

$quotation = ":"  
$source = "$DLetter$quotation\Users\$associd\Music"
$destination = "C:\Users\$env:USERNAME\Music"
$copy = "/COPY:DTO"
$files = "*.*"
$backup = "/B"
$reportextrafiles = "/X"
$verboseoutput = "V"
$fullpathname = "FP"
$mirror = "/MIR"
$backupmode = "/ZB"
$subfolders = "/E"
$Retries = "/R:2"
$consolewrite = "/TEE"

$files = [array][System.IO.Directory]::EnumerateFiles($source, '*.*', 'AllDirectories')
$index = 0
$total = $files.Count
$starttime = $lasttime = Get-Date

robocopy.exe $source $destination $files $backup $reportextrafiles $verboseoutput $fullpathname $mirror $backupmode $subfolders $Retries $consolewrite | ForEach-Object -Process {
    $index++
    $currtime = (Get-Date) - $starttime
    $avg = $currtime.TotalSeconds / $index
    $last = ((Get-Date) - $lasttime).TotalSeconds
    $left = $total - $index
    $WrPrgParam = @{
        Activity = (
            "music_Transfer $(Get-Date -f s)",
            "Total: $($currtime -replace '\..*')",
            "Avg: $('{0:N2}' -f $avg)",
            "Last: $('{0:N2}' -f $last)",
            "ETA: $('{0:N2}' -f ($avg * $left / 60))",
            "min ($([string](Get-Date).AddSeconds($avg*$left) -replace '^.* '))"
        ) -join ' '
        Status = "$index of $total ($left left) [$('{0:N2}' -f ($index / $total * 100))%]"
        #CurrentOperation = "FILE: $file"
        PercentComplete = $index / $total * 100
        Id = 1
    }
    Write-Progress @WrPrgParam
    $lasttime = Get-Date

    $data = $_.Split([char]9);
    if (($data.Count -gt 4) -and ("$($data[4])" -ne ""))
    {
        $file = "$($data[4])"
        Write-Progress "Percentage $($data[0])" -Activity "Music" -CurrentOperation "$($file)" -ErrorAction SilentlyContinue; 
    }
    else
    {
        Write-Progress "Percentage $($data[0])" -Activity "Music" -CurrentOperation "$($file)"
    }
}
# Robocopy has a bitmask set of exit codes, so only complain about failures:
[int] $exitCode = $global:LastExitCode;
[int] $someCopyErrors = $exitCode -band 8;
[int] $seriousError = $exitCode -band 16;
if (($someCopyErrors -ne 0) -or ($seriousError -ne 0))
{
    Write-Error "ERROR: robocopy failed with a non-successful exit code: $exitCode"    
}
}
}