使用Copy-WithProgress Powershell函数的文件大小总计计算问题

时间:2018-09-28 08:39:41

标签: powershell robocopy

我采用了Trevor Sullivan编写的PowerShell脚本Copy-WithProgress(谢谢!),但是其中有8,000个文件返回3PB,5,000个文件返回7Bytes的计算似乎不正确。

我将以下Robocopy参数用于我的“登台日志”:

$CommonRobocopyParams = '/MIR /S /NP /NDL /NC /BYTES /NJH /NJS /R:1 /W:1'

并且正在使用以下内容读取日志以计算要复制的字节:

$BackupLog = 'C:\Users\username\Desktop\Backup_2018-09-20-112849'
$StagingLogPath = '{0}\robocopy staging.log' -f $BackupLog
$StagingContent = Get-Content -Path $StagingLogPath | ? {$_} #Skip blank lines in log
$TotalFileCount = $StagingContent.Count                    

Write-Host "Number of selected files to be copied: $TotalFileCount."

$RegexBytes = '(?<=\s+)\d+(?=\s+)' 

[RegEx]::Matches(($StagingContent -join "`n"), $RegexBytes) | % { $BytesTotal = 0 } { $BytesTotal += $_.Value }

$message = 'Total number of bytes to be copied: {0:N0}' -f ($BytesTotal/1GB)
Write-Host $message 

上面的“跳过日志中的空白行”是为了处理来自另一个文件夹的另一组日志,即同一“登台日志”文件中的文档之间用空白行分隔。

该脚本在我测试过的6台计算机中有1台计算错误,并且仅在将台式机备份到有问题的计算机上时才计算错误。我检查了“登台日志”,并将其与如下所示的正确计算进行了比较:

13162   C:\Users\username\Desktop\File 1.xlsx           
1765924 C:\Users\username\Desktop\File 2.xlsx           
68838   C:\Users\username\Desktop\File 3.pdf            
2380    C:\Users\username\Desktop\File 4.docx           
403759  C:\Users\username\Desktop\File 5.txt            
10068   C:\Users\username\Desktop\File 6.xlsx           
28502   C:\Users\username\Desktop\File 7.jpg            
139277  C:\Users\username\Desktop\File 8.docx           
553469  C:\Users\username\Desktop\File 9.log            
283502  C:\Users\username\Desktop\Book1.xlsx            

我很好奇是否有人遇到了这个问题并解决了这个问题。预先谢谢你!

1 个答案:

答案 0 :(得分:1)

很确定这里的陷阱是您使用System.Int32。对于很大的数字,数学将失败。

考虑一些支持较大数字的内容,例如[int64][long][double]。例如,(1pb).GetType().FullName的网络是System.Int64

% { [int64]$BytesTotal = 0 }

在初始化时进行一次强制转换应该足以解决所有错误的计算。

如果愿意,可以在GitHub上查看我的Copy-WithRobocopyProgress函数,以获取乐趣。使用作业和robocopy在PowerShell中显示进度