更改脚本以检查驱动器的剩余空间

时间:2016-04-02 13:30:57

标签: vbscript

我目前有这个VBScript,用于检查文件夹的大小,然后删除文件(最早的文件),直到达到阈值。

Option Explicit
'use next line for production
On Error Resume Next 
'use next line for debugging
'On Error GoTo 0

Dim strResult: strResult = Wscript.ScriptName
Dim objFSO, strOldestFile, dtmOldestDate, strFolder, oFolder, intFolderSize

Set objFSO = CreateObject("Scripting.FileSystemObject")
dtmOldestDate = Now
strFolder     = "C:\Users\PLEX\Downloads\Torrent\"
strOldestFile = "" 
Set oFolder   = objFSO.GetFolder(strFolder)

intFolderSize = Int(((oFolder.Size / 1024) / 1024) / 1024)

Do While intFolderSize >= 110
  strOldestFile = ""
  dtmOldestDate = Now

  FindOldestFile oFolder

  'WScript.Echo strOldestFile
  objFSO.DeleteFile strOldestFile, True
  strResult = strResult & vbNewLine & dtmOldestDate & vbTab & strOldestFile

  intFolderSize = Int(((oFolder.Size / 1024) / 1024) / 1024)
Loop

'WScript.Echo strResult
WScript.Quit

Sub FindOldestFile(objFolder)
  Dim objFile, colFiles, colFolders, strFile, dtmFileDate
  'find oldest file
  Set colFiles = objFolder.Files
  For Each objFile In colFiles
    strFile = objFile.Path
    dtmFileDate = objFile.DateCreated
    If dtmFileDate < dtmOldestDate Then
      dtmOldestDate = dtmFileDate
      strOldestFile = strFile
    End If
  Next
  'recurse subfolders
  Set colFolders = objFolder.SubFolders
  For Each objFile In colFolders
    FindOldestFile objFile
  Next
End Sub

目前此脚本将:

  1. 检查文件夹A的容量,包括所有子文件夹。
  2. 从文件夹A和子文件夹中删除文件(最早的文件),直到达到阈值(在本例中为110GB)
  3. 我想修改脚本来执行此操作:

    1. 检查文件夹A的容量,包括所有子文件夹。
    2. 从文件夹B(文件夹A的子文件夹)和子文件夹中删除文件(最早的文件),直到达到阈值。
    3. 例如,是否可以让此脚本检查整个C:驱动器的已用容量,并在C上剩余少于5%时从C:\Users\PLEX\Downloads\Torrent\删除最旧的文件:?

      修改:我已获得完全访问权限,并通过将C:\更改为C:来修正错误。我还将C:\Users\PLEX\Desktop\New Folder更改为\Users\PLEX\Desktop\New Folder,现在没有错误。唯一的问题是IntFolderSize仍然显示我的子文件夹的大小,而不是根。我已使用Wscript.Echo intfoldersize确认了这一点。我目前的脚本如下。如何让IntFolderSize显示根的使用大小?

      option explicit
      'use next line for production
      'On Error Resume Next 
      'use next line for debugging
      'On Error GoTo 0
      
      Dim strResult: strResult = Wscript.ScriptName
      Dim objFSO, strOldestFile, dtmOldestDate, strFolder, oFolder, intFolderSize
      Set objFSO = CreateObject("Scripting.FileSystemObject")
          dtmOldestDate = Now
          strFolder = "C:"
          strOldestFile = "" 
      Set oFolder = objFSO.GetFolder( strFolder)
      
      intFolderSize = Int(((oFolder.Size / 1024) / 1024) / 1024)
      
      Wscript.Echo intfoldersize
      
      Do While intFolderSize >= 70
        strOldestFile = ""
        dtmOldestDate = Now
      
      FindOldestFile objFSO.GetFolder(objFSO.BuildPath(strFolder,"\Users\PLEX\Desktop\New Folder\"))
      
      'wscript.echo strOldestFile
        objFSO.DeleteFile strOldestFile, True
        strResult = strResult & vbNewLine & dtmOldestDate & vbTab & strOldestFile 
      
        intFolderSize = Int(((oFolder.Size / 1024) / 1024) / 1024)
      
      Loop
      
      'Wscript.Echo strResult
      Wscript.Quit
      
      Sub FindOldestFile( objFolder)
        Dim objFile, colFiles, colFolders, strFile, dtmFileDate
        ' find oldest file
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
          strFile = objFile.Path
          dtmFileDate = objFile.DateCreated
          If dtmFileDate < dtmOldestDate Then
            dtmOldestDate = dtmFileDate
            strOldestFile = strFile
          End If
        Next
        ' recurse subfolders
        Set colFolders = objFolder.SubFolders
        For Each objFile in colFolders
          FindOldestFile objFile
        Next
      End Sub
      

1 个答案:

答案 0 :(得分:0)

更改

FindOldestFile oFolder

FindOldestFile objFSO.GetFolder(objFSO.BuildPath(strFolder, "sub\folder"))

其中strFolder是您的根文件夹,"sub\folder"是该根文件夹下的子文件夹的相对路径。

修改:请注意oFolder.Size属性会引发错误,除非您有权访问oFolder下面的每个文件和文件夹。如果您想要检查卷上的可用磁盘空间,最好使用WMI并严格使用oFolder来删除要删除的文件夹:

strFolder   = "C:\Users\PLEX\Desktop\New folder"
Set oFolder = objFSO.GetFolder(strFolder)

threshold = ...    'desired minimum free diskspace in byte

Set wmi = GetObject("winmgmts://./root/cimv2")
qry = "SELECT * FROM Win32_Volume WHERE DriveLetter='C:'"

Do While wmi.ExecQuery(qry).ItemIndex(0).FreeSpace < threshold
  ...
  FindOldestFile oFolder
  ...
Loop