Directory size monitoring with VBScript
OK, I don’t admit to being anything like an expert with VBScript or Windows Scripting Host but I’m learning. I’ve bought a couple of books on the subject to help me administer the network at work and automate repetitive tasks.
We have a folder on the network attached storage server that is used for archiving aged data. Our tape backup solution uses 100GB tapes. If this archiving folder goes above 100GB, the data has to be archived off on to two or more tapes which is a nightmare to administer. I’m sure you can understand how long it takes to backup 100GB, even over 1Gbps backbone but inserting tapes to do a restore, even from a backup set that spans two tapes is the biggest pain in the backside.
I need something to check the size of this archive folder and didn’t want to invest in some naff program to do it for me. I need to check it maybe once a day so I can just run a scheduled task once a day, no expenditure needed, no other resources etc.
I stumbled across a VBScript yesterday that had all the fundamental bits of code to do what I wanted it to do so I adjusted it slightly to check the folder I wanted it to check and email me if it was over a certain size or it encountered a problem. I guess the script could also be adapted to monitor server shares and availability too since if it can’t connect it will send you an email telling you so. The obvious drawback is the fact that it can’t email you telling you that the email server is down! 😉
sFolderPath = "\\server\share"
Set oFSObject = CreateObject("Scripting.FileSystemObject") '**** Create the instance of the file system object
If oFSObject.FolderExists(sFolderPath) Then '**** If the folder exists
Set oFSFolder = oFSObject.GetFolder(sFolderPath) '**** Get the folder object
If Not oFSFolder Is Nothing Then '**** If we successfully got the folder
'42949672960 = 40GB
'96636764160 = 90GB
'102005473280 = 95GB
If oFSFolder.Size > 102005473280 Then
sMsg = "This email has been sent by VBScript." & vbcrlf _
& vbcrlf & "The folder " & sFolderPath & " is greater than 95GB." & vbcrlf _
& vbcrlf & "An archive should be created within the next week to avoid exceeding the 100GB tape limit."
'**** Something went wrong trying to get the folder, perhaps permissions aren't set properly, check the rights
sMsg = "The folder " & sFolderPath & " could not be retrieved. Please make sure you have the appropriate network permissions and that the folder is properly shared."
'**** The folder doesn't exist, it's not shared or there is network communication trouble
sMsg = "The folder " & sFolderPath & " could not be retrieved. Please check the folder's path (" & sFolderPath & ") and the local area network connection."
If Len(sMsg) > 0 Then
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Size Warning (95GB)"
objMessage.Sender = "myserver @mydomain.com"
objMessage.From = "myserver @mydomain.com"
objMessage.To = "email@example.com"
objMessage.TextBody = sMsg
'==This section provides the configuration information for the remote SMTP server.
'==Normally you will only change the server name or IP.
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.mydomain.com"
'Server port (typically 25)
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'==End remote SMTP server configuration section==
Set oFSFolder = Nothing
Set oFSObject = Nothing