繁体   English   中英

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

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

我有一个包含文件夹名称列表的文本文件。

我想从我的目录中删除名称出现在文本文件中的文件夹

这是我的尝试

#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

这给了我文本文件和目录中存在的文件夹名称列表。 现在我想删除此列表中的文件夹

希望有人可以帮助

谢谢

你可以使用

#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