在 powershell 中计算 robocopy 输出的匹配数

在 powershell 中计算 robocopy 输出的匹配数

我有以下 robocopy 命令行:

robocopy $drv "B:\Blending Produções - Backup\Blending Produções - Tyrone Hirt - BC" /e /NJH /NDL /NC /BYTES /MT:4 /l /xd /A-:SH $IgnorarPastas /xf $IgnorarArquivos

该命令将按照既定参数列出所有必须复制的文件。

重点是:我想找到一种方法来计算将复制多少个文件。

为此,我尝试了一种计算 robocopy 输出中有多少个匹配“T:”的方法,但我没有得到预期的结果......

这是我的完整脚本:

$drv = "T:\"
$IgnorarPastas = "03. Proxy", "`$Recycle.Bin"
$IgnorarArquivos = "*.avi", "*.ini"

robocopy $drv "B:\Blending Produções - Backup\Blending Produções - Tyrone Hirt - BC" /e /NJH /NDL /NC /BYTES /MT:4 /l /xd /A-:SH $IgnorarPastas /xf $IgnorarArquivos
            
$robocopyCommand = "robocopy $drv ""B:\Blending Produções - Backup\Blending Produções - Tyrone Hirt - BC"" /e /NJH /NDL /NC /BYTES /MT:4 /l /xd /A-:SH $IgnorarPastas /xf $IgnorarArquivos"
$filesToCopy = Invoke-Expression $robocopyCommand
$numberOfFiles = ($filesToCopy | Select-String -Pattern "T:\\" -AllMatches).Matches.Count
            
write-host "`nNumber of files to be copied " -NoNewline -f White; write-host "$numberOfFiles" -NoNewline -f Green;

这是控制台中脚本的输出:

$drv = "T:\"
 $IgnorarPastas = "03. Proxy", "`$Recycle.Bin"
 $IgnorarArquivos = "*.avi", "*.ini"

robocopy $drv "B:\Blending Produções - Backup\Blending Produções - Tyrone Hirt - BC" /e /NJH /NDL /NC /BYTES /MT:4 /l /xd /A-:SH $IgnorarPastas /xf $IgnorarArquivos


$comandoRobocopy = "robocopy $drv ""B:\Blending Produções - Backup\Blending Produções - Tyrone Hirt - BC"" /e /NJH /NDL /NC /BYTES /MT:4 /l /xd /A-:SH $IgnorarPastas /xf $IgnorarArquivos"


$robocopyCommand = "robocopy $drv ""B:\Blending Produções - Backup\Blending Produções - Tyrone Hirt - BC"" /e /NJH /NDL /NC /BYTES /MT:4 /l /xd /A-:SH $IgnorarPastas /xf $IgnorarArquivos"
$filesToCopy = Invoke-Expression $robocopyCommand
$numberOfFiles = ($filesToCopy | Select-String -Pattern "T:\\" -AllMatches).Matches.Count

    
    write-host "`nNumber of files to be copied " -NoNewline -f White; write-host "$numberOfFiles" -NoNewline -f Green;

               12680    T:\_Ferramenta Backup\Backup Local\_\BP - Backup Script - Root of this device - Get Backup.ps1
100%  
            379568522   B:\Blending Produ‡äes - Backup\Blending Produ‡äes - Tyrone Hirt - BC\Blending Produ‡äes\02. Clientes\2023\Grupo Teste\Teste Car\_Imagens\Drone\DJI_0001.MOV
            2380021041  B:\Blending Produ‡äes - Backup\Blending Produ‡äes - Tyrone Hirt - BC\Blending Produ‡äes\02. Clientes\2023\Grupo Teste\Teste Car\_Imagens\Drone\DJI_0002.MOV
            43588300    B:\Blending Produ‡äes - Backup\Blending Produ‡äes - Tyrone Hirt - BC\Blending Produ‡äes\02. Clientes\2023\Grupo Teste\Teste Car\_Imagens\Drone\DJI_0003.MOV
            3335677673  B:\Blending Produ‡äes - Backup\Blending Produ‡äes - Tyrone Hirt - BC\Blending Produ‡äes\02. Clientes\2023\Grupo Teste\Teste Car\_Imagens\Drone\DJI_0004.MOV
            2325513829  B:\Blending Produ‡äes - Backup\Blending Produ‡äes - Tyrone Hirt - BC\Blending Produ‡äes\02. Clientes\2023\Grupo Teste\Teste Car\_Imagens\Drone\DJI_0005.MOV
            3245228768  B:\Blending Produ‡äes - Backup\Blending Produ‡äes - Tyrone Hirt - BC\Blending Produ‡äes\02. Clientes\2023\Grupo Teste\Teste Car\_Imagens\Drone\DJI_0006.MOV
             1392290    B:\Blending Produ‡äes - Backup\Blending Produ‡äes - Tyrone Hirt - BC\Blending Produ‡äes\02. Clientes\2023\Grupo Teste\Fenauto 2023\Geral\_Pr\Adobe Premiere Pro Auto-Save\Fenauto Geral 1--aa12bd8f-a378-c373-6d52-62bab88e86a6-2023-12-04_11-36-34.prproj
             1398658    B:\Blending Produ‡äes - Backup\Blending Produ‡äes - Tyrone Hirt - BC\Blending Produ‡äes\02. Clientes\2023\Grupo Teste\Fenauto 2023\Geral\_Pr\Adobe Premiere Pro Auto-Save\Fenauto Geral 1--aa12bd8f-a378-c373-6d52-62bab88e86a6-2023-12-04_12-12-44.prproj
            54138464    B:\Blending Produ‡äes - Backup\Blending Produ‡äes - Tyrone Hirt - BC\Blending Produ‡äes\02. Clientes\2023\Grupo Teste\Teste\11. Novembro\07. Teste I Fenauto Overview Final\01. Fluxo\01. Ae\07-12-2023 Teste I Fenauto Overview Final (converted
).aep
            84566896    B:\Blending Produ‡äes - Backup\Blending Produ‡äes - Tyrone Hirt - BC\Blending Produ‡äes\02. Clientes\2023\Grupo Teste\Teste\00. Identidade\01. Fluxo\01. Ae\Adobe After Effects Auto-Save\Teste I Identidade Audiovisual (converted) auto-
save 5.aep
            230815274   B:\Blending Produ‡äes - Backup\Blending Produ‡äes - Tyrone Hirt - BC\Blending Produ‡äes\02. Clientes\2023\Grupo Teste\Teste\11. Novembro\17. DLPL Cast - EP 15\05. Render\Vertical\DLPL Cast I EP 14 I Corte 1 I Vert I Financiamento I Final - v
01.mp4
            230450405   B:\Blending Produ‡äes - Backup\Blending Produ‡äes - Tyrone Hirt - BC\Blending Produ‡äes\02. Clientes\2023\Grupo Teste\Teste\11. Novembro\17. DLPL Cast - EP 15\05. Render\Vertical\DLPL Cast I EP 14 I Corte 2 I Vert I Consigna‡Æo I Final - v01
.mp4
               12200    B:\Blending Produ‡äes - Backup\Blending Produ‡äes - Tyrone Hirt - BC\Blending Produ‡äes\02. Clientes\2023\Grupo Teste\Teste Car\12. Dezembro\29. TesteCar I Carros e Repasse\01. Fluxo\01. Ae\02-01-2024 Nome.aep
           12200                  136490    B:\Blending Produ‡äes - Backup\Blending Produ‡äes - Tyrone Hirt - BC\Blending Produ‡äes\02. Clientes\2023\Grupo Teste\Teste Car\12. Dezembro\29. TesteCar I Carros e Repasse\01. Fluxo\02. Pr\02-01-2024 Nome.prproj

------------------------------------------------------------------------------

                   Total   Copiada      IgnoradaIncompatibilidade     FALHA      Extras
Diret¢rios:          3407      3302          3406         0         1           6
 Arquivos:         19705         1         19707         0         0          14
    Bytes: 2037206266068     12680 2037651251411         0         0 12312508810
N.§ de Vezes:       0:00:00   0:00:00                           0:00:00     0:00:00
   Finalizado em: quinta-feira, 4 de janeiro de 2024 01:19:19


Number of files to be copied 169

我无法通过这种方式获得正确的计数,结果是 169,而它应该是 1,因为在输出中仅复制了一个文件。

我该如何调整脚本以获得预期的结果?

编辑

我正在尝试另一种方法:

# Find the line containing "Arquivos:"
$filesLine = $filesToCopy | Select-String -Pattern "Arquivos:.*?(\d+)\s+(\d+)" | ForEach-Object { $_.Matches }

# Extract the second group of numbers from the line
$numberOfFilesCopied = $filesLine.Groups[2].Value

在此代码中,正则表达式Arquivos:.*?(\d+)\s+(\d+)用于搜索包含“Arquivos:”的行并捕获两组数字。然后使用 访问第二组$filesLine.Groups[2].Value

我还是无法得到正确的结果。

简而言之,我需要在开始复制之前计算 robocopy 将复制多少个文件。

答案1

我发现您的脚本存在两个潜在问题:

  • 两次调用 robocopy,这可能会导致第二次调用的结果不同,这绝对是没有必要的
  • 的使用Invoke-Expression,一般不建议使用,类似于其他编程语言中的类似代码评估函数。出于安全原因,我建议避免使用它,这很可能在这里并不重要,但值得一提的是,几乎总是有更好的替代品。请参阅文档欲了解更多信息,例如:

Invoke-Expression在脚本中使用 cmdlet 时,请采取合理的预防措施。使用Invoke-Expressioncmdlet 运行用户输入的命令时,请在运行命令之前验证该命令是否可以安全运行。通常,最好使用预定义的输入选项来设计脚本,而不是允许自由格式的输入。


如果您想静默运行它,我只需替换两者:

robocopy $drv $destination /e /NJH /NDL /NC /BYTES /MT:4 /l /xd /A-:SH $IgnorarPastas /xf $IgnorarArquivos
            
$robocopyCommand = "robocopy $drv $destination /e /NJH /NDL /NC /BYTES /MT:4 /l /xd /A-:SH $IgnorarPastas /xf $IgnorarArquivos"

和:

$robocopyResult = robocopy $drv $destination /e /NJH /NDL /NC /BYTES /MT:4 /l /xd /A-:SH $IgnorarPastas /xf $IgnorarArquivos

并且stdout流将保存到变量中。然后,如果需要,您可以进一步操作,例如在末尾stderr添加静音流。2>$null

答案2

($filesToCopy | Select-String -Pattern "T:\\").Count在逻辑中省略-AllMatches.Matches部分以获得与匹配的正确计数Robocopy日志存储在数组变量中。

另外,定义$robocopyCommand变量时,用单引号而不是双引号括起来,并删除双引号转义字符。

电源外壳

$drv = "T:\"
$IgnorarPastas = "03. Proxy", "`$Recycle.Bin"
$IgnorarArquivos = "*.avi", "*.ini"

robocopy $drv "B:\Blending Produções - Backup\Blending Produções - Tyrone Hirt - BC" /e /NJH /NDL /NC /BYTES /MT:4 /l /xd /A-:SH $IgnorarPastas /xf $IgnorarArquivos
            
$robocopyCommand = 'robocopy $drv "B:\Blending Produções - Backup\Blending Produções - Tyrone Hirt - BC" /e /NJH /NDL /NC /BYTES /MT:4 /l /xd /A-:SH $IgnorarPastas /xf $IgnorarArquivos'
$filesToCopy = Invoke-Expression $robocopyCommand
$numberOfFiles = ($filesToCopy | Select-String -Pattern "T:\\").Count
            
write-host "`nNumber of files to be copied " -NoNewline -f White; write-host "$numberOfFiles" -NoNewline -f Green;

相关内容