Vbscript VB脚本文件集合

Vbscript VB脚本文件集合,vbscript,Vbscript,我正在研究一些逻辑,如果磁盘空间达到定义的最大容量,将从特定文件夹中删除文件,我有以下代码: 'Remove files if disk space falls below 100GB While hDisk.FreeSpace < 100000000000 Set Directory = Fso.GetFolder("C:\backups") Set Files = Directory.Files

我正在研究一些逻辑,如果磁盘空间达到定义的最大容量,将从特定文件夹中删除文件,我有以下代码:

    '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

重新发明轮子。您正在尝试创建一个脚本来执行Windows中已经存在的任务。

嗯,我不认为该代码能够满足您的需要。比较遇到的前两个文件,然后删除旧文件。但如果这两个文件恰好是该文件夹中所有文件中的最新文件,该怎么办?在删除任何内容之前,您确实需要对目录中的所有文件进行排序。(您不能指望Files集合如何对文件排序,因为这是任意的。)它使用file1作为参考变量对两个文件进行比较,然后将file1设置为比较的两个文件中较新的文件,并删除较旧的文件。我会发布一些伪代码以使它不那么混乱。你是否考虑过WSHELSE?运行或.Excel A“DR/O:D…”并从该列表中删除?@ EkkHurd -不,谢谢你的建议。我需要更多地使用它,我想在做任何改进之前先运行一个脚本。你能为评论部分保留评论吗?否则,请启发我,我愿意学习能够完成这项特定任务的替代方案;我在上面的问题中指出了这一点。谢谢
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
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