Check folder to see if files are being updated

I want to create a script similar to the one on this page:

http://www.evalesco.com/check-any-file-older-x-minutes-directory

I want to check the folder every 120 minutes for every file and see if there is a file that hasn't been updated.  The files I'm looking for end in ".enc".

I would like to have an email alert created if there is an outdated file.
williamss132Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KimputerCommented:
It would look something like this:

' This script processes the list of files in the directory’s subfolders (i.e. recursively)  passed as argument
' and returns 1 if any file is older than "age_threshold" minutes

' File age threshold in minutes
Dim age_threshold
age_threshold = 120

Dim old_file_not_found
old_file_found = 0

scanFolder("c:\temp\")

if old_file_found = 1 then
	Sendmail
end if


sub scanFolder(objFolder)

Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.getFolder(objFolder)

For Each file in f.Files
Dim age
age = DateDiff("n", file.DateLastModified, Now)
If (age > age_threshold) and (lcase(right(file.name,4) = ".enc") Then
old_file_found = 1
old_file_name = file
Exit For
End If
Next

if old_file_found = 0 Then
dim subfolderPath, ObjSubfolder
For each subfolderPath in f.SubFolders
Set ObjSubfolder = fso.GetFolder(subfolderPath)
scanFolder(ObjSubfolder)
Set ObjSubfolder = nothing
Next
end if

end sub

Sub Sendmail()
	Set objMessage = CreateObject("CDO.Message") 

	'Send Message
	strToEmailAddress = "test@test.com"
	strFromEmailAddress = "oldfilescanner@test.com"
	strSMTPServer = "real.smtp.server.com"
	strSMTPServerPort = 25
	
	objMessage.Subject = "Old file found: " & old_file_name
	objMessage.From = "Old file scanner <" & strFromEmailAddress & ">"""
	objMessage.To = strToEmailAddress
	objMessage.TextBody = "Old file found: " & old_file_name
	objMessage.Configuration.Fields.Item _
	("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
	objMessage.Configuration.Fields.Item _
	("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPServer
	objMessage.Configuration.Fields.Item _
	("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = strSMTPServerPort
	objMessage.Configuration.Fields.Update
	objMessage.Send

End Sub

Open in new window


Some vars need to be adjusted in the email section. It's based on CDO and needs a functioning smtp server ready to accept mail from the machine the script runs on, so change these things:

scanFolder("c:\temp\")
strToEmailAddress = "test@test.com"
strFromEmailAddress = "oldfilescanner@test.com"
strSMTPServer = "real.smtp.server.com"
strSMTPServerPort = 25
0
williamss132Author Commented:
Looks great!

So, I get an email when I run it.  But there is no file name in the Subject or Body of the email.
0
KimputerCommented:
Yes there is:

      objMessage.Subject = "Old file found: " & old_file_name
      objMessage.From = "Old file scanner <" & strFromEmailAddress & ">"""
      objMessage.To = strToEmailAddress
      objMessage.TextBody = "Old file found: " & old_file_name

As you can see, it's only one file name. Do you prefer the script to look for ALL files (older than 120 mins) ?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

williamss132Author Commented:
I would like it to loop through all of the files in the folder and send one email listing all of the outdated files.  

Thanks Kimputer!
0
KimputerCommented:
Changed to mail a list of all files found:

' This script processes the list of files in the directory’s subfolders (i.e. recursively)  passed as argument
' and returns 1 if any file is older than "age_threshold" minutes

' File age threshold in minutes
Dim old_file_name
Dim age_threshold
age_threshold = 120

Dim old_file_not_found
old_file_found = 0

scanFolder("c:\temp\")

if old_file_found = 1 then
	Sendmail
end if


sub scanFolder(objFolder)

Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.getFolder(objFolder)

For Each file in f.Files
Dim age
age = DateDiff("n", file.DateLastModified, Now)
If (age > age_threshold) and (lcase(right(file.name,4) = ".enc")) Then
	old_file_found = 1
	old_file_name = old_file_name & vbCrlf & file
End If
Next

dim subfolderPath, ObjSubfolder
For each subfolderPath in f.SubFolders
	Set ObjSubfolder = fso.GetFolder(subfolderPath)
	scanFolder(ObjSubfolder)
	Set ObjSubfolder = nothing
Next


end sub

Sub Sendmail()
	Set objMessage = CreateObject("CDO.Message") 

	'Send Message
	strToEmailAddress = "test@test.com"
	strFromEmailAddress = "oldfilescanner@test.com"
	strSMTPServer = "real.smtp.server.com"
	strSMTPServerPort = 25
	
	objMessage.Subject = "Old file(s) found!"
	objMessage.From = "Old file scanner <" & strFromEmailAddress & ">"""
	objMessage.To = strToEmailAddress
	objMessage.TextBody = "Old file found: " & old_file_name
	objMessage.Configuration.Fields.Item _
	("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
	objMessage.Configuration.Fields.Item _
	("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPServer
	objMessage.Configuration.Fields.Item _
	("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = strSMTPServerPort
	objMessage.Configuration.Fields.Update
	objMessage.Send

End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
williamss132Author Commented:
Thanks for this excellent solution!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.