簡體   English   中英

如何使用PowerShell遞歸刪除具有數字名稱的文件夾

[英]How can I recursively delete folder with numerical names using PowerShell

我有一些腳本,可以根據其修改日期刪除文件夾。 如果文件夾名稱中僅包含數字,有人可以幫我刪除嗎?

$locations=import-csv "C:\Temp\Scripts\AgeOffDirsGeneral.csv"

foreach ($location in $locations)
{

    $Source=$location.Source

    Get-ChildItem  $source  |Where-Object  {$_.psiscontainer}  | Foreach-Object {Remove-Item  -Recurse  -Force $_.FullName}

}

Remove-Item接受管道輸入,因此不需要ForEach。
如果輸出看起來正常,請刪除-WhatIf

locations=import-csv "C:\Temp\Scripts\AgeOffDirsGeneral.csv"

foreach ($location in $locations){

    $Source=$location.Source

    Get-ChildItem  $source -Dir -Recurse|
      Where-Object  { $_.Name -match '^\d+$'}  | 
        Remove-Item -Force -WhatIf
}

順便說一下,可以避免中間變量$ source直接插入$ location.Source

我能夠使用-like標簽解決此問題。

$locations=import-csv "C:\Temp\Scripts\AgeOffDirsGeneral.csv"
foreach ($location in $locations)
{
    $Source=$location.Source
    Get-ChildItem  $source  | Where-Object {  $_.psiscontainer -and $_.Name -like '*[0-9]*' } | Foreach-Object {Remove-Item  -Recurse  -Force $_.FullName}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM