2011-05-03 49 views
0

我工作的一些邏輯,如果磁盤空間達到規定的最大容量會從特定文件夾中刪除的文件,我有以下代碼:VB腳本文件的集合

'Remove files if disk space falls below 100GB 
     While hDisk.FreeSpace < 100000000000 
      Set Directory = Fso.GetFolder("C:\backups") 
      Set Files = Directory.Files 
      Dim file1 
      Dim file2 
      For n = Files.Count - 1 to 0 Step - 1 
      If IsEmpty(file1) or IsNull(file1) Then 
ERROR Here -->Set file1 = Files.Item(n) 
      ElseIf n > 0 Then 
       Set file2 = Files.Item(n) 
       If hDisk.FreeSpace > 100000000000 Then 
        Exit For 
       ElseIf file2.DateLastModified < file1.DateLastModified And DateDiff("D", file2.DateLastModified, Now) > 7 Then 
        file2.Delete 
       ElseIf file1.DateLastModified < file2.DateLastModified And DateDiff("D", file1.DateLastModified, Now) > 7 Then 
        file1.Delete 
        Set file1 = Files.Item(n) 
       Else 
        'Nothing 
       End If 
      Else 
       'Nothing 
      End If 
      Next 
     Wend 'End loop when drive is below max capacity 

什麼它應該做的循環播放文件夾中的文件集合,並刪除最早的文件,直到磁盤空間達到容量以上。所以有一些文件比較必須完成。我正遇到上述行中的無效過程調用或參數錯誤(請參閱評論)。我也想知道這是否是最好的方法,我願意提供更好的建議,最好能減少代碼。

UPDATE:

嘗試添加設置,在賦值語句的前面,但仍然得到同樣的錯誤。

更新2(WORKING !! SCRIPT):

發揮它多一點,並能拿到劇本的工作,在這裏它的全部的情況下,任何人想要使用它。我添加了註釋來指示自定義值,可以安全地假定任何其他類似的值也可以自定義,即磁盤大小在多個位置中定義。

Dim Fso 
Dim hDisk 
Dim Directory 
Dim Files 
Dim myArray() 

Set Fso = CreateObject("Scripting.FileSystemObject") 
Set hDisk = Fso.GetDrive("c:") 'Custom Value - drive to monitor 

If hDisk.FreeSpace < 100000000000 Then 
    'Delete files until free space is below max capacity (defined here as 100GB) 
    While hDisk.FreeSpace < 100000000000 'Custom Value - disk size in bytes 
     Set Directory = Fso.GetFolder("C:\backups") 'Custom Value - Directory to monitor 
     Set Files = Directory.Files 
     Redim myArray(Files.Count) 
     i=0 
     For Each fl in Files 
      Set myArray(i)=fl 
      i=i+1 
     Next 

     Dim file1 
     Dim file2 
     For n = Files.Count - 1 to 0 Step - 1 
      '1st PASS: Instantiate first file 
      If IsEmpty(file1) or IsNull(file1) Then 
       Set file1 = myArray(n) 
      'Compare 1st file with next file and current date, remove oldest if it's older than a week 
      ElseIf n > 0 Then 
       Set file2 = myArray(n) 
       If hDisk.FreeSpace > 100000000000 Then 
        Exit For 
       ElseIf file2.DateLastModified < file1.DateLastModified And DateDiff("D", file2.DateLastModified, Now) > 7 Then 'Custom Value - File age in number of days 
        file2.Delete 
       ElseIf file1.DateLastModified < file2.DateLastModified And DateDiff("D", file1.DateLastModified, Now) > 7 Then 
        file1.Delete 
        Set file1 = myArray(n) 
       Else 
        'Nothing 
       End If 
      'Remove remaining file if it's older than a week 
      Else 
       Set file1 = myArray(n) 
       If DateDiff("D", file1.DateLastModified, Now) > 7 Then 
       file1.Delete 
       End If 
      End If 
     Next 
    Wend 'End loop when drive is below max capacity 
End If 

更新3:

爲了澄清什麼正在做的,僞代碼如下:

If disk space is maxed 
    While disks space is maxed 
    For each file 
    If 1st File is empty 
     Get 1st File 
     If disk space is below max 
     Exit 
     Else Get Next File 
     If Next File is older than 1st File and older than a week 
     Delete Next File 
     Continue 
     Else if 1st File is older and older than a week 
     Delete current 1st File 
     Set 1st File to Next File 
     Continue 
     Else if 1st file is the only file and is older than a week 
     Delete 1st File 
+0

恩,我不認爲代碼會做你想做的。您比較遇到的前兩個文件,然後刪除較舊的文件。但是,如果這兩個文件碰巧是該文件夾中所有文件的最新版本呢?在刪除任何內容之前,您確實需要對目錄中的所有文件進行排序。 (您不能指望Files集合如何訂購文件,因爲它是任意的。) – Adventure 2011-05-03 19:44:53

+0

它將兩個文件進行比較,使用file1作爲參考變量,然後將file1設置爲被比較的兩個文件中較新的一個,並刪除舊的。我會發布一些僞代碼,以減少混淆。 – kingrichard2005 2011-05-03 19:59:40

+0

您是否考慮將WshShell.Run或.Exec作爲「DIR/O:D ...」並從該列表中刪除? – 2011-05-03 20:12:34

回答

1

重新發明輪子。您正嘗試創建一個腳本來執行Windows中已存在的任務。

+2

您能否爲留言部分保留評論?否則,請賜教,我願意瞭解可以完成這項具體任務的替代方案;我在上面的問題中指出了這一點。謝謝。 – kingrichard2005 2011-05-04 16:28:11