Avatar of iNc0g
iNc0g
Flag for Israel asked on

Script to notify administrator if file older than 48 hours

Hi,

OS: Server 2003 SP2

I need a script/schedule task to notify me somehow (email/alert) when file c:\xxx.dat  is older than 48 hours , is there a way to accomplish this ?

Thanks.
Windows BatchVB ScriptScripting Languages

Avatar of undefined
Last Comment
RobSampson

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
rscottvan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
iNc0g

ASKER
I ran the script in order to test it, therefor I changed:

If DateDiff("H",oFile.DateLastModified,Now) > 48 then

To:

If DateDiff("M",oFile.DateLastModified,Now) > 2 then

So it's supposed to identify 2 minutes difference and email me, but it didn't ..

In those lines I kept the < > signs:
oEMail.Textbody = "<Please check file>"
"<191.191.191.191>"

What could be the problem ?
doesn't the script require user and pwd in order to run .. ?

Thanks!
SOLUTION
RobSampson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
iNc0g

ASKER
Many thanks, you guys are great.
1 last thing, how do I add an action to email if the file does not exist at all ?
rscottvan

This could be made a little more elegant, but should do the trick.  

And...  remove all the brackets.  < >

Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
If oFSO.FileExists("c:\xxx.dat") then
	Dim oFile : Set oFile = oFSO.GetFile("c:\xxx.dat")
	If DateDiff("H",oFile.DateLastModified,Now) > 48 then
		Dim oEMail
		Set oEMail = CreateObject("CDO.Message")
		oEMail.From = "myscript@mydomain.com"
		oEMail.To = "myemail@mydomain.com"
		oEMail.Subject = "File c:\xxx.dat age exceeds 48 hours"
		oEMail.Textbody = "enter body here"
		oEMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
		oEMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "enter mail server IP Address here" 
		oEMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
		oEMail.Configuration.Fields.Update
		oEMail.Send
	End If
Else
	Dim oEMail
	Set oEMail = CreateObject("CDO.Message")
	oEMail.From = "myscript@mydomain.com"
	oEMail.To = "myemail@mydomain.com"
	oEMail.Subject = "File c:\xxx.dat does not exist"
	oEMail.Textbody = "enter body here"
	oEMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
	oEMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "enter mail server IP Address here" 
	oEMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
	oEMail.Configuration.Fields.Update
	oEMail.Send

End If

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
RobSampson

Hi, this should work a little cleaner.

Regards,

Rob.

Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")

strFile = "C:\xxx.dat"

Set oEMail = CreateObject("CDO.Message")
oEMail.From = "myscript@mydomain.com"
oEMail.To = "myemail@mydomain.com"
If oFSO.FileExists(strFile) then
	Dim oFile : Set oFile = oFSO.GetFile(strFile)
	If DateDiff("H",oFile.DateLastModified,Now) > 48 then
		oEMail.Subject = "File " & strFile & " age exceeds 48 hours"
		oEMail.Textbody = "enter body here"
	End If
Else
	oEMail.Subject = "File " & strFile & " does not exist"
	oEMail.Textbody = "enter body here"
End If
oEMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
oEMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "enter mail server IP Address here" 
oEMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
oEMail.Configuration.Fields.Update
oEMail.Send

Open in new window