2009-07-21 53 views

回答

2

要預定任務,您可以使用任務調度器(example here

對於一個腳本,你可以使用

param($WorkDirectory = 'c:\work' 
    , $LogFile = 'c:\work\deletelog.txt') 

#Check to see if there is enough free space 
if (((Get-WmiObject -Query "SELECT FreeSpace FROM Win32_LogicalDisk WHERE DeviceID = 'C:'").FreeSpace/1GB) -lt 5) 
{ 
    #Get a list of the folders in the work directory 
    $FoldersInWorkDirectory = @(Get-ChildItem $WorkDirectory | Where-Object {$_ -is [System.IO.DirectoryInfo]} | Sort-Object -Property LastWriteTime -Descending) 
    $FolderCount = 0 

    while (((Get-WmiObject -Query "SELECT FreeSpace FROM Win32_LogicalDisk WHERE DeviceID = 'C:'").FreeSpace/1GB) -lt 5) 
    { 
      #Remove the directory and attendant files and log the deletion 
     Remove-Item -Path $FoldersInWorkDirectory[$FolderCount].FullName -Recurse 
      "$(Get-Date) Deleted $($FoldersInWorkDirectory[$FolderCount].FullName)" | Out-File -Append $LogFile 

      $FolderCount++ 
    } 
} 
0

那麼,這一切取決於你的文件夾是如何「使用」。如果沒有簡單的指標,則可嘗試使用.NET FileSystemWatcher類查找更改並記住它們以及隊列中的時間戳,按訪問時間排序。然後,您可以選擇要從該隊列中刪除的下一個文件夾。但它絕對不是很漂亮。

相關問題