如何使用powershell检查重复的多个文件?
我想检查重复的文件。如果文件的状态像这样,则表示重复。名称相同但扩展名不同如何使用powershell检查重复的多个文件?,powershell,Powershell,我想检查重复的文件。如果文件的状态像这样,则表示重复。名称相同但扩展名不同 AAA18WWQ6BT602.PRO AAA18WWQ6BT602.XML 我可以用我的脚本解决这个问题。但如果我有超过1.XML的文件,我就有问题了 AAA18WWQ6BT602.PRO AAA18WWQ6BT602.XML AAA18WWQ6BT601.XML AAA18WWQ6BT604.XML 在这种情况下,它不会检测到文件AAA18WWQ6BT602.PRO和AAA18WWQ6BT602.XML重复。 任何
AAA18WWQ6BT602.PRO
AAA18WWQ6BT602.XML
我可以用我的脚本解决这个问题。但如果我有超过1.XML的文件,我就有问题了
AAA18WWQ6BT602.PRO
AAA18WWQ6BT602.XML
AAA18WWQ6BT601.XML
AAA18WWQ6BT604.XML
在这种情况下,它不会检测到文件AAA18WWQ6BT602.PRO
和AAA18WWQ6BT602.XML
重复。
任何人都可以帮我。
谢谢
-Include
参数仅在Get ChildItem
上的路径以\*
结尾或使用了-Recurse
开关时有效
以下操作应满足您的要求:
$flagFolder = 'D:\*'
$dupeReport = 'D:\Flag_Duplicate.txt'
$duplicates = Get-ChildItem -Path $flagFolder -File -Include '*.xml', '*.pro' |
Group-Object -Property BaseName | Where-Object { $_.Count -gt 1 }
if ($duplicates) {
# output the duplicate XML to Flag_Duplicate.txt
$duplicates.Group | Where-Object {$_.Extension -eq '.xml' } | ForEach-Object {
$_.FullName | Out-File -FilePath $dupeReport -Append
}
# do the rest of your code
Write-Host "Flag duplicated, continue for Error_Monitoring"
Error_Monitoring
}
else {
Write-Host "Flag does not duplicate, continue the process"
}
您的脚本没有正确迭代。您需要有一个迭代来检查。在我看来,
测试路径
逻辑看起来很混乱。我尽量保留你的代码
此脚本根据任何后缀副本检查任意xml basename文件名(不仅仅是pro
):
@SBR不客气。我现在注意到,这本可以做得更有效。只有在将所有文件加载到数组中时才能执行
Get ChildItem
。接下来,您将筛选xml文件。这将为您节省一个文件系统迭代,这个迭代往往很慢。谢谢@tukan。如果你不介意的话,我可以帮你回答我的另一个问题。谢谢。
$flagFolder = 'D:\*'
$dupeReport = 'D:\Flag_Duplicate.txt'
$duplicates = Get-ChildItem -Path $flagFolder -File -Include '*.xml', '*.pro' |
Group-Object -Property BaseName | Where-Object { $_.Count -gt 1 }
if ($duplicates) {
# output the duplicate XML to Flag_Duplicate.txt
$duplicates.Group | Where-Object {$_.Extension -eq '.xml' } | ForEach-Object {
$_.FullName | Out-File -FilePath $dupeReport -Append
}
# do the rest of your code
Write-Host "Flag duplicated, continue for Error_Monitoring"
Error_Monitoring
}
else {
Write-Host "Flag does not duplicate, continue the process"
}
$Flag_Path = "C:\dir_to_be_checked"
$xmlFilesArray = @()
$allFilesExceptXml = @() # all files excluding xml files
# Get all the xml files
Get-ChildItem -Path $Flag_Path -Include "*.xml" | ForEach-Object { $xmlFilesArray += $_.basename }
# Get all files from the directory the xml files
Get-ChildItem -Path $Flag_Path -Exclude "*.xml" | ForEach-Object { $allFilesExceptXml += $_.basename }
# Iterate over list of files names without suffix
ForEach ($xmlFile in $xmlFilesArray) {
ForEach ($fileToCheck in $allFilesExceptXml) {
If ($xmlFile -eq $fileToCheck) {
# logging the duplicate file (specifying utf8 or the output would be UTF-16)
Write-Output "$Flag_Path\$xmlFile.xml" | Out-File -Append -Encoding utf8 $Flag_Path\Flag_Duplicate
Write-Host "Flag duplicated, continue with duplicate search"
# pause
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Error_Monitoring
} Else {
Write-Host "Flag is not duplicated. Continue with the search."
}
}
}