如何在大量文件夹中搜索两种文件并比较它们的大小?
How do i search a large amount of folders for two kinds of files and compare their sizes?
我有很多结构不同的文件夹,我想搜索将存在于同一文件夹中的两个文件扩展名。我需要比较这两个文件的大小。
Get-ChildItem -Path C:\SharedFolders\ -Recurse | ?{$_.Extension -eq ".qbw"}
我可以得到第一项,但我不确定如何告诉它与同一目录中的文件进行比较
我应该将它传递给另一个命令还是尝试使用数组?
您需要一个 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
}
}
我有很多结构不同的文件夹,我想搜索将存在于同一文件夹中的两个文件扩展名。我需要比较这两个文件的大小。
Get-ChildItem -Path C:\SharedFolders\ -Recurse | ?{$_.Extension -eq ".qbw"}
我可以得到第一项,但我不确定如何告诉它与同一目录中的文件进行比较
我应该将它传递给另一个命令还是尝试使用数组?
您需要一个 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
}
}