如何搜索大量文件夹中的两种文件并比较它们的大小?

时间:2016-07-08 15:52:25

标签: powershell-v3.0

我有很多不同结构的文件夹,我想搜索将存在于同一文件夹中的两个文件扩展名。我需要比较这两个文件的大小。

Get-ChildItem -Path C:\SharedFolders\ -Recurse | ?{$_.Extension -eq ".qbw"}

我可以获得第一项,但我不知道如何告诉它与同一目录中的文件进行比较

我应该将它传递给另一个命令还是尝试使用数组?

1 个答案:

答案 0 :(得分:0)

你需要一个ForEach-Object循环,例如:

$root = 'C:\SharedFolders'
Get-ChildItem -Path $root -Filter '*.qbw' -Recurse | ForEach-Object {
  $tlg = Get-ChildItem $_.Directory -Filter '*.tlg' | Select -First 1
  if ($tlg) {
    Compare-Object $_ $tlg
  }
}

如果.tlg和.qbw文件具有相同的基本名称,请使用以下内容:

$root = 'C:\SharedFolders'
Get-ChildItem -Path $root -Filter '*.qbw' -Recurse | ForEach-Object {
  $tlgpath = $_.FullName -replace '\.qbw$', '.tlg'
  If (Test-Path -LiteralPath $tlgpath) {
    $tlg = Get-Item $tlgpath
    Compare-Object $_ $tlg
  }
}