简体   繁体   English

Powershell - 如果名称存在于文本文件中,则删除文件夹

[英]Powershell - delete folder if name exists in text file

I have a text file with a list of folder name.我有一个包含文件夹名称列表的文本文件。

I want to delete the folders from my directory for which the name appears in the text file我想从我的目录中删除名称出现在文本文件中的文件夹

Here is my try这是我的尝试

#I get the contents of the text file

$textfile = Get-Content "C:\Users\s611284\Desktop\Dossiers.txt"

#I get the name of the folders from the directory

$Foldersname = Get-ChildItem -Path $Directory | ForEach-Object { 
        $_.BaseName 
      } 

#I get the names present in the text file and in the folders of the directory

Compare-Object -IncludeEqual $textfile $Foldersname | Where-Object SideIndicator -eq '==' 

93 / 5 000 93 / 5 000

This gives me the list of folder names present in the text file and in the directory.这给了我文本文件和目录中存在的文件夹名称列表。 Now I would like to delete folders present in this list现在我想删除此列表中的文件夹

Hope someone can help希望有人可以帮助

Thank you谢谢

You could use你可以使用

#I get the contents of the text file

$textfile = Get-Content "C:\Users\s611284\Desktop\Dossiers.txt"

#I get the name of the folders from the directory

$Foldersname = Get-ChildItem -Path $Directory | ForEach-Object { 
        $_.BaseName 
      } 

#I get the names present in the text file and in the folders of the directory

Compare-Object -IncludeEqual $textfile $Foldersname | Where-Object SideIndicator -eq '=='|ForEach-Object {

    $item=$_.inputobject
    remove-item $Directory\$item -Recurse  
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM