title: VB Script Files Collection
body: <p>I'm working on some logic that will remove files from a particular folder if the disk space reaches a defined max capacity, I have the following code:</p>
  
  <pre><code>    'Remove files if disk space falls below 100GB
          While hDisk.FreeSpace &lt; 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 --&gt;Set file1 = Files.Item(n)
              ElseIf n &gt; 0 Then
                  Set file2 = Files.Item(n)
                  If hDisk.FreeSpace &gt; 100000000000 Then 
                      Exit For
                  ElseIf file2.DateLastModified &lt; file1.DateLastModified And DateDiff("D", file2.DateLastModified, Now) &gt; 7 Then 
                      file2.Delete
                  ElseIf file1.DateLastModified &lt; file2.DateLastModified And DateDiff("D", file1.DateLastModified, Now) &gt; 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
  </code></pre>
  
  <p>What it's supposed to do is loop through a collection of files in a folder and remove the oldest file(s) until the disk space is above capacity.  So there is some file comparison that must be done.  I'm encountering a <strong>Invalid procedure call or argument</strong> error on the line above (see comment).  I'd also like to know if this is the best approach, I'm open to better suggestions, preferably ones that will cut down on code.</p>
  
  <p>UPDATE:</p>
  
  <p>Tried adding Set, in front of the assignment statement, but still get the same error.</p>
  
  <p>UPDATE 2 (WORKING SCRIPT!!):</p>
  
  <p>Played with it a bit more and was able to get the script working, here it is in its entirety in case anyone else wants to use it.  I added comments to indicate custom values, it can be safely assumed that any other similar value is also customizable, i.e. the disk size is defined in multiple locations.</p>
  
  <pre><code>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 &lt; 100000000000 Then
      'Delete files until free space is below max capacity (defined here as 100GB)
      While hDisk.FreeSpace &lt; 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 &gt; 0 Then
                  Set file2 = myArray(n)
                  If hDisk.FreeSpace &gt; 100000000000 Then 
                      Exit For
                  ElseIf file2.DateLastModified &lt; file1.DateLastModified And DateDiff("D", file2.DateLastModified, Now) &gt; 7 Then 'Custom Value - File age in number of days
                      file2.Delete
                  ElseIf file1.DateLastModified &lt; file2.DateLastModified And DateDiff("D", file1.DateLastModified, Now) &gt; 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) &gt; 7 Then
                  file1.Delete
                  End If
              End If
          Next
      Wend 'End loop when drive is below max capacity
  End If
  </code></pre>
  
  <p>UPDATE 3:</p>
  
  <p>To clarify what's being done, the pseudocode is as follows:</p>
  
  <pre><code>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
  </code></pre>