Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recursive folder search for a certain file size w/e-mail

File and Data Processing

Recursive folder search for a certain file size w/e-mail

by  computerhighguy  Posted    (Edited  )
I would like to thank smurfhell for his unbeknownst help on getting this script working. I had to reference one of his posts to get this working. Anyway, this particular script searches for ldf files for an SQL database and if it greater than 2GB, it send me an e-mail. You can easily change this to any extension by modifying
Code:
If lcase(Right(objFile.Name,3)) = "ldf" Then

to have any extension. You can also modify the following line for other file sizes.

Code:
round(objFile.Size/1073741824,1)

KB = round(objFile.Size/1024,1)
MB = round(objFile.Size/1048576,1)
GB = round(objFile.Size/1073741824,1)

' // **************************************
' // ComputerHighGuys recursive search
' //
' // Date Created: 20 Aug 07
' //
' // Modify the SMTP Server for your SMTP
' // server. Also change the notifyEmail
' // address to whatever e-mail addresses
' // you want to be notified. Set the
' // debug to False when you put it into
' // production.
' // **************************************
SMTPServer = "mail.domain.com"
notifyEmail = "User1@domain.com;user2@domain.com"
setDebug = "True"

' // Directory where the SQL Server is installed
sqlDir = "D:\Program Files\Microsoft SQL Server\"
fileList = ""
set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(sqlDir)
Set colFiles = objFolder.Files

ScanSubFolders(objFolder)

if fileList <> "" then
If setDebug = "True" then
wscript.echo fileList
End If
Call SendErrorMessage(notifyEmail,fileList)
End if

Sub scanSubFolders(objFolder)

Set colFolders = objFolder.SubFolders

For Each objSubFolder In colFolders
Set colFiles = objSubFolder.Files

For Each objFile in colFiles
' // Looking for SQL Database LDF files.

If lcase(Right(objFile.Name,3)) = "ldf" Then

' // Getting File size in GB
If round(objFile.Size/1073741824,1) > 2 then
If setDebug = "True" then
wscript.echo objFile.Name & " " & round(objFile.Size/1073741824,1) & "GB"
End if
fileList = objFile.Name & " " & round(objFile.Size/1073741824,1) & "GB" & vbCRLF & vbCRLF & fileList
End if

End If
Next
ScanSubFolders(objSubFolder)
Next

End Sub



Sub SendErrorMessage (strDestEmail,errorMsg)

Set objEmail = CreateObject("CDO.Message")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject ("WScript.Shell")

If IsNull(strDestEmail) Then
If debugMode = "True" then
Wscript.Echo "No email address, no message sent."
End If
Exit Sub
End If

objEmail.From = "Server@domain.com"
objEmail.To = strDestEmail
objEmail.Subject = "Database log files that are greater than 2 GB"
objEmail.Textbody = errorMsg

objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServer
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send

End Sub
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top