![當路徑可以是 50 個中的任一個時,使用 PowerShell 5 或 > 刪除目錄/子目錄](https://rvso.com/image/1618355/%E7%95%B6%E8%B7%AF%E5%BE%91%E5%8F%AF%E4%BB%A5%E6%98%AF%2050%20%E5%80%8B%E4%B8%AD%E7%9A%84%E4%BB%BB%E4%B8%80%E5%80%8B%E6%99%82%EF%BC%8C%E4%BD%BF%E7%94%A8%20PowerShell%205%20%E6%88%96%20%3E%20%E5%88%AA%E9%99%A4%E7%9B%AE%E9%8C%84%2F%E5%AD%90%E7%9B%AE%E9%8C%84.png)
Windows 10 64 位元。電源外殼 5.1
當路徑可以是 50 個中的任一個時,使用 PowerShell 5 或 > 刪除目錄/子目錄。
從中刪除livebolivar.com
及其子目錄any of 50 folders named mmddyy
"%USERPROFILE%\desktop\websites\"any of 50 folders named mmddyy"\livebolivar.com"
刪除%USERPROFILE%\desktop\websites\011920\livebolivar.com
發現太多:
gci %USERPROFILE%\desktop\websites -recurse | Where-Object {($_.PSIsContainer)} | Foreach { if ( $_.Name -eq "livebolivar.com") {remove-item $_.fullname -confirm}}
不工作:
gci -exclude favorites %USERPROFILE%\desktop\websites -recurse | Where-Object {($_.PSIsContainer)} | Foreach { if ( $_.Name -eq "livebolivar.com") {remove-item $_.fullname -confirm}}
這刪除了子資料夾:
$path= @("%USERPROFILE%\desktop\websites\*\livebolivar.com")
$folders= gci -path $path -Recurse | Where-Object {$_.PsIsContainer} |Group-Object {$_.FullName.Split('_')[0] }
ForEach($folder in $folders){
$folder.Group | % { Remove-Item $_.fullname -recurse -force}}
製作測試資料夾:
pushd %USERPROFILE%\Desktop
foreach($i in -10..-1){
$z=(Get-Date).AddDays($i).tostring("MMddyy")
ni -itemtype directory $z\livebolivar.com\New Folder > $null}
ni -itemtype file $z\livebolivar.com\New Folder\test.txt > $null}
popd
exit
遞歸目錄刪除目錄使用remove-item遞歸刪除目錄和子目錄使用ri刪除目錄和子目錄
答案1
Windows 10 64 位元。電源外殼 5.1
使用通配符遞歸刪除目錄。
gci $env:USERPROFILE\desktop\websites\*\livebolivar.com | % {ri $_.fullname -recurse -force -whatif}
% 對輸入物件集合中的每個項目執行操作。
$_ 表示管道中的目前項目。
.fullname 是路徑\名稱.ext
沒有子目錄嗎?-force
不需要
gci $env:USERPROFILE\desktop\websites\*\livebolivar.com | % {ri $_.fullname -recurse -whatif}