gpt4 book ai didi

powershell - 如何使用Powershell检查重复的多个文件?

转载 作者:行者123 更新时间:2023-12-02 23:07:29 25 4
gpt4 key购买 nike

我要检查重复的文件。如果文件的条件是这样,则意味着重复。名称相同,但扩展名不同。

AAA18WWQ6BT602.PRO
AAA18WWQ6BT602.XML

我可以用我的脚本弄清楚这种情况。但是如果我有多个这样的.XML文件,我就会遇到问题
AAA18WWQ6BT602.PRO
AAA18WWQ6BT602.XML
AAA18WWQ6BT601.XML
AAA18WWQ6BT604.XML

在这种情况下,它将不会检测到 AAA18WWQ6BT602.PROAAA18WWQ6BT602.XML文件重复。
任何人都可以帮助我。
谢谢
$duplicate = @()
@(Get-ChildItem "$Flag_Path\*.xml") | ForEach-Object { $duplicate += $_.basename }

if(Test-Path -Path "$Flag_Path\*$duplicate*" -Exclude *.xml)
{
Get-ChildItem -Path "$Flag_Path\*$duplicate*" -Include *.xml | Out-File $Flag_Path\Flag_Duplicate
Write-Host "Flag duplicated, continue for Error_Monitoring"
pause
Error_Monitoring
}
else{
Write-Host "Flag does not duplicate, continue the process"
}

最佳答案

您的脚本未正确迭代。您需要进行迭代检查。 Test-Path逻辑对我来说很困惑。我试图保留尽可能多的代码。

此脚本检查是否有任何后缀重复的xml基本名文件名(不仅是pro):

$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."
}
}
}

关于powershell - 如何使用Powershell检查重复的多个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58641951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com