Powershell:优化脚本按顺序将大量文件(<2MB)排列到文件夹中

时间:2018-03-14 20:28:53

标签: powershell sorting pdf optimization 7zip

此脚本在200,000多个大约2MB的文件上执行。这些文件需要安排在最大数量为$ max的文件夹中。然后他们将被压缩。现在,下面的代码有效,但可能很慢。我有什么建议来优化这个脚本?我不确定正则表达式是否可以进行排序。欢迎任何优化建议。谢谢!

$src = "C:\srcPath"
$dst = "C:\dstPath"
Set-Alias 7z "C:\Path\7za.exe"
$max = 1000
$Container = "Files"
$ZipFileName = "Files.zip" 
$fileFilter = "*.pdf"
$fileCount = (dir $src $fileFilter| measure).Count

if ([int]$fileCount -gt 0) {
  md $dst\$Container
  $folderCount = [Math]::Ceiling([int]$fileCount / [int]$max)

  for ($n = 1; $n -le $folderCount; $n++) {
    if(!(test-path $dst\$Container\$n)){
      md $dst\$Container\$n
      Get-ChildItem -Path $src\* -File -Include $fileFilter | Sort-Object { [regex]::Replace($_.Name,'\d+',{ $args[0].Value.PadLeft(20) }) } | Select-Object -First $max | Copy-Item -Destination $dst\$Container\$n | Out-Null
    }
  }
  7z a $dst\$ZipFileName $dst\$Container | Out-Null
}

0 个答案:

没有答案
相关问题