2017-06-02 181 views
0

下面的腳本查找E:\WebDelivery\MagicOS目錄中的文件夾。如果此目錄中的任何文件夾超過14天,則會刪除它們及其子目錄。它不包括被命名​​3210刪除二級子目錄

文件,我需要的腳本改爲只在子目錄查找修改日期,如果該日期是14天以上,就需要刪除這些文件夾及其內容,但離開S6.15,T7.12只能輸入父目錄。

這裏的文件夾結構: E:\WebDelivery\MagicOS - >>下這是目錄S6.15, T7.12, S5.6等,並在每個那些有目錄中的所有以數字作爲名稱1,2,3等,並可能是一個文件名爲upd.info.raf

E\WebDelivery\MagicOS ->> S6.15 
         ->> 1 
         ->> 2 

當前腳本正在檢查S6.15級文件夾的修改日期,並刪除該文件夾及其子目錄(如果它們超過14天)。我需要檢查號碼子目錄1,2,3等修改日期,並刪除那些如果他們比14天老,但離開父母S6.15目錄。基本上我不在乎S6.15文件夾上的日期只有數量子文件夾1,2,3等...

這是我難倒的地方。任何想法都會有所幫助。這是現有的腳本。

$Today = Get-Date 
    $DaysToKeep = 14 
    $TargetFolder = "E:\WebDelivery\MagicOS" 
    $LastWrite = $Today.AddDays(-$DaysToKeep) 
    $excludeFolders = @("testdir","testdir2") 

    $Folders = Get-Childitem –path $Targetfolder | 
    Where {$_.psIsContainer} | 
    Where {$_.LastWriteTime -le "$LastWrite"} | 
    Where {$excludeFolders -notcontains $_.Name} 

    If ($Folders) { 
     foreach ($Folder in $Folders) { 
      Write-Host "Processing Folder $($Folder.Name)" 
      $items = Get-ChildItem -Path $Folder.FullName -Exclude 
    "upd.info.raf" -Recurse 
      If ($items) { 
     Write-Host "Removing: $($Folder.Name)" 
       # $items | Remove-Item -Recurse -Verbose 
      } Else { 
       Write-Host "There are no items to remove in $($Folder.Name)" 
      } 
     } 
    } Else { 
    Write-Host "There are no folders to empty." 
    } 

回答

0

我實際上能夠自己弄清楚這一點。我添加了一個foreach循環來處理子目錄的每個子項目並刪除舊的文件夾。

下面是修改後的腳本:

$Today = Get-Date 
$DaysToKeep = 14 
$TargetFolder = "E:\WebDelivery\testdir" 
$LastWrite = $Today.AddDays(-$DaysToKeep) 
$excludeFolders = @("testdir","testdir2") 

$Folders = Get-Childitem –path $Targetfolder | 
Where {$_.psIsContainer} | 
Where {$excludeFolders -notcontains $_.Name} 

If ($Folders) { 
foreach ($Folder in $Folders) { 
     Write-Host "Processing Folder $($Folder.Name)" 
     $items = Get-ChildItem -Path $Folder.FullName -Exclude 
"upd.info.raf" | Where {$_.LastWriteTime -le "$LastWrite"} 
     If ($items) { 
     foreach ($item in $items){ 
     Write-Host "Removing: $($item.FullName)" 
     $item | Remove-Item -Recurse -Verbose 
     } 

     } Else { 
      Write-Host "There are no items to remove in $($Folder.Name)" 
     } 
    } 
} Else { 
Write-Host "There are no folders to empty."