VBS – Check Folder/File size

Below is an example off a VBS script to monitor the size of a directory or a file. If the size is larger then the size specified it will mail a message with a warning.

Script to monitor a folder:

strFolderToMonitor = "C:\TEST"
intSizeInGB = 12
intSizeInBytes  = (intSizeInGB * 1024 * 1024 * 1024)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolderToMonitor) 

If objFolder.Size > intSizeInBytes Then
  Set objMessage = CreateObject("CDO.Message")
  objMessage.From = "[email protected]"
  objMessage.To = "[email protected]"
  objMessage.Subject = "Folder " & strFolderToMonitor & " exceeded " & intSizeInGB & "GB"
  objMessage.TextBody = "The folder " & strFolderToMonitor & " has exceeded " & intSizeInGB & "GB.  Please panic in an orderly fashion." 

  objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.domain.suffix"
  objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
  objMessage.Configuration.Fields.Update 

  objMessage.Send
End If

 

Script to monitor a file:

strFileToMonitor = "C:\TEST\TEST.TXT"
intSizeInGB = 30
intSizeInBytes  = (intSizeInGB * 1024 * 1024 * 1024)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strFileToMonitor)

'If objFolder.Size > intSizeInBytes Then
If objFile.Size > intSizeInBytes Then
  Set objMessage = CreateObject("CDO.Message")
  objMessage.From = "[email protected]"
  objMessage.To = "[email protected]"
  objMessage.Subject = "File " & strFileToMonitor & " exceeded " & intSizeInGB & "GB"
  objMessage.TextBody = "The file " & strFileToMonitor & " has exceeded " & intSizeInGB & "GB." & chr(10) & chr(13) & "Please panic in an orderly fashion." 

  objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.domain.suffix"
  objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
  objMessage.Configuration.Fields.Update 

  objMessage.Send
End If

 

Or download the files here:

[wpdm_file id=”19″]

[wpdm_file id=”20″]

Leave a Reply

Your email address will not be published. Required fields are marked *