С помощью Powershell я пытаюсь удалить некоторые файлы в каталоге. У меня есть массив, который содержит имена файлов, которые я хочу сохранить (они находятся в массиве $ NameOfFilesToKeep). Все остальные файлы должны быть удалены. Вот код, который я использую:
####### YOU HAVE TO MODIFY THIS SETTINGS #######
$GenericIDandNameOfProject = "[19123]PARIS EST" #If the name of your files is something like this [19123]PARIS EST_XXX.XML , it should be equal to "[19123]PARIS EST"
$Entries = "10
8
9
16" #Be careful, this " should be on the same line as the last number!
####### END OF THE SETTINGS YOU HAVE TO MODIFY #######
$IDofInterestingElements = $Entries.split("`n")
for ($i=0; $i -lt $IDofInterestingElements.Length-1; $i++) {
$IDofInterestingElements[$i] = $IDofInterestingElements[$i].Substring(0,$IDofInterestingElements[$i].Length-1)
}
Write-Output ("Soooo, you want " + $IDofInterestingElements.Length + " files at the end")
$NameOfFilesToKeep = @()
foreach ($ID in $IDofInterestingElements) {
$NameOfFilesToKeep += $GenericIDandNameOfProject + "_" + $ID + ".XML"
}
$CurrentDirectory = (Get-Location).path
foreach ($file in dir $CurrentDirectory) {
if ($NameOfFilesToKeep.Contains($file.name)) {
Write-Output (${file})
}
else {
Remove-Item "${CurrentDirectory}\${file}"
}
}
Start-Sleep 100
Название файлов: «[19123] PARIS EST_XXX. XML», где XXX - это число, например [19123] PARIS EST_10. XML
Это просто не работает. Я думаю, что скучаю по чему-то важному в части Remove-Item. Что может быть не так с моим кодом?
Редактировать
Я добавил полный код для лучшего понимания.