我有一些包含很多文件的文件夹。我想检查这些文件夹中是否存在该文件。 如果该文件存在于某个文件夹中,则意味着我在该文件夹中有重复的文件。重复的文件名称不完全相同,但是我可以从文件名的一部分中识别出该文件。 确定文件名后,我将删除其中之一。
我尝试过此操作,可以确定哪个文件夹包含所需的文件。但是我不知道如何删除它。
$Path = ".\Folder"
$Path_File = (Get-Item (Get-ChildItem "$Path\*\*.pro")).Directory.FullName
$Path_File
$GetFilePro = Get-ChildItem -Name $Path_File -Filter *.pro*
$SubFile = $GetFilePro.Substring(7, 35)
$SubFile
$GetDupFile = Get-ChildItem -Name $Path_File -Filter *.que* |
Where-Object {$_.Contains("$SubFile")}
$GetDupFile
Remove-Item $GetDupFile
$FindJobpros = Get-ChildItem -Name "$OpJob_Path\*\*.pro\"
ForEach ($FindJobpro in $FindJobpros)
{
$SubFile = $FindJobpros.Substring(7, 35)
$GetDupFile = Get-ChildItem -Path $OpJob_Path\*\*.que | Where-Object {$_.Name -Match "$SubFile"}
Remove-Item $GetDupFile
}
}
for(;;)
{
[xml]$Config_File = Get-Content .\Config.xml
Function Remove_Jobpro
{
$FindJobpros = Get-ChildItem -Name "$OpJob_Path\*\*.pro\"
Write-Output "Job.pro : $FindJobpros"
$SubFile = $FindJobpros.Substring(7, 35)
# $FilterJob = Get-ChildItem -path "$OpJob_Path\*\*.que" -Filter *$sub*
foreach($sub in $SubFile)
{
$n = 0
While (-not(Get-ChildItem -path "$OpJob_Path\*\*.que" -Filter *$sub*)){
Start-Sleep -s 5
$n++
(Get-ChildItem -path "$OpJob_Path\*\*.que" -Filter *$sub*)
Write-Output "Attempt no $n"
}
Write-Output "Job match found after $n attempts"
$GetMatch = Get-ChildItem -path "$OpJob_Path\*\*.que" -Filter *$sub*
$GetMatch
Remove-Item $GetMatch
}
}
Function Check_Jobpro
{
$OpJob_Path = "D:\Auto\"
$n = 0
while (-not (Test-Path -Path "$OpJob_Path\*\*.pro\")) {
Start-Sleep -s 5
$n++
(Test-Path -Path "$OpJob_Path\*\*.pro\")
Write-Output "Attempt no $n"
}
Write-Output "Job.pro found after $n attempts"
Write-Output "Continue to delete the file"
Remove_Jobpro
}
$Flag_Path = $Config_File.Automation_Config.Path.Flag_Path
if(Test-Path -Path "$Flag_Path\*.XML")
{
Write-Host "Flag Found"
$Flag = Get-ChildItem -Name "$Flag_Path\*.XML"
$FlagName = $Flag.ToString().Substring(0,14)
$FlagName
Copy-Item "$Flag_Path\*.XML" -Destination "$Flag_Path\$FlagName.PRO"
Copy-Item "$Flag_Path\*.XML" -Destination "$Flag_Path\$FlagName.PRO"
Remove-Item "$Flag_Path\*.XML"
}
else {
Write-Host "Flag not found, continue delete duplicate job"
Write-Output "Check .pro file in all folder"
Check_Jobpro
}
}
示例文件
20110813055741_XX_ABC11AABCDEFGH_X1.que
SN_IDC_20110813055741_XX_ABC11AABCDEFGH_X1.pro
如果找到,我将删除此20110813055741_XX_ABC11AABCDEFGH_X1.que
答案 0 :(得分:1)
尽管您的代码有时使我感到困惑,但我认为这是您想要做的。
您似乎想重命名.xml文件的新要求,该文件位于从配置文件读取的文件夹中。由于这不是您最初的问题,因此我不会做得太深。
$OpJob_Path = "D:\Auto\" #"# the root folder where .pro and .que files should be
$MaxAttempts = 5
#first try and get an array of .pro filenames without extension and having the first 7 characters stripped off
$n = 0
while ($true) {
$n++
Write-Host "Attempt no $n finding .pro files"
$ProFiles = @(Get-ChildItem -Path $OpJob_Path -Filter '*.pro' -File -Recurse | ForEach-Object { $_.BaseName.Substring(7) })
# exit the endless loop if we found .pro files OR the number of attempts have reached the maximum
if ($ProFiles.Count -gt 0 -or $n -gt $MaxAttempts) { break }
Start-Sleep -Seconds 5
}
if ($ProFiles.Count) {
# next, try and find .que files where the BaseName can be found in the $ProFiles array
$n = 0
while ($true) {
$n++
Write-Host "Attempt no $n finding .que files"
$QueFiles = @(Get-ChildItem -Path $OpJob_Path -Filter '*.que' -File -Recurse | Where-Object { $ProFiles -contains $_.BaseName })
# exit the endless loop if we found .que files OR the number of attempts have reached the maximum
if ($QueFiles.Count -gt 0 -or $n -gt $MaxAttempts) { break }
Start-Sleep -Seconds 5
}
# if .que files with similar names were found, remove them
if ($QueFiles.Count) {
Write-Host "Job match files found after $n attempts"
foreach ($que in $QueFiles) {
Write-Host "Removing file '$($que.FullName)'"
$que | Remove-Item -Force -WhatIf
}
}
else {
Write-Warning "No Job match files (.QUE) found after $n attempts.."
}
}
else {
Write-Warning "No Job files (.PRO) found after $n attempts.."
}
然后,您的代码中就有XML内容,如果我理解正确的话,那就是将xml文件重命名为.Pro文件。
为此,我认为这会更容易:
[xml]$Config_File = Get-Content .\Config.xml
$Flag_Path = $Config_File.Automation_Config.Path.Flag_Path
if (Test-Path -Path $Flag_Path) {
Write-Host "Flag Found"
Get-ChildItem -Path $Flag_Path -Filter '*.xml' -File | ForEach-Object {
$newName = '{0}.pro' -f ($_.BaseName.Substring(0,14))
Write-Host "Renaming file '$($_.FullName)' to '$newName'"
$_ | Rename-Item -NewName $newName -WhatIf
}
}
else {
Write-Host "Flag not found."
}
注意:在Remove-Item
和Rename-Item
cmdlet上,我都添加了一个-WhatIf
开关。使用该开关,不会删除或重命名任何内容。 PowerShell将显示将发生的情况,因此您可以首先检查是否达到了预期。
如果您对该输出感到满意,请从代码中删除WhatIf
开关,以便删除和/或重命名文件。