當路徑可以是 50 個中的任一個時,使用 PowerShell 5 或 > 刪除目錄/子目錄

當路徑可以是 50 個中的任一個時,使用 PowerShell 5 或 > 刪除目錄/子目錄

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} 

相關內容