[英]Delete DIR recursively if file match? (Powershell)
我想知道是否有人知道如何刪除目錄中的指定文件? 例如,如果具有以下目錄:
PS C:\Users\mike> dir
Directory: C:\Users\mike
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 9/17/2009 6:26 PM 6615 pic001.jpg
-a--- 9/19/2009 9:58 AM 7527 notes.txt
-a--- 8/31/2009 5:03 PM 10506 Project.xlsx
我想刪除。\\ mike,如果其中有jpg文件,而其他目錄中有.jpg文件。 如果目錄中沒有指定文件,則不應刪除該文件。
到目前為止,我所擁有的是:
get-childitem "C:\Users\mike" -include *.jpg -recurse | Where-Object { $_.mode -like 'd*' } | remove-item
當對* .jpg使用Get-ChildItem時,只會獲取文件-除非您有一個名為{something} .jpg的目錄。 順便說一句,我將使用-filter並遠離-include參數。 塔爾是龍-請參閱有關該參數的文檔。
這應該為您解決問題:
Get-ChildItem 'C:\Users\Mike' *.jpg -r | Foreach {$_.Directory} |
Remove-Item -Recurse -Verbose -WhatIf
如果鍵入,請使用別名將其縮短為:
gci 'C:\Users\Mike' *.jpg -r | %{$_.Directory} | ri -r -v -wh
當您對結果滿意時,請刪除-WhatIf以使其真正刪除目錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.