Link to home
Start Free TrialLog in
Avatar of cjb123
cjb123

asked on

Windows Command Line Script - File exists for > 5 mins

Hello,

I'm looking to build a script that would send an alert email if there are files within a given directory for > 5 mins.  I'm a bit of a noob when it comes to scripting in Windows either through batch scripts or cscripts, any help would be appreciated.
Avatar of Bill Prew
Bill Prew

Unless you wanted to have a script running all the time and "sleeping" in between checks, you'd probably want to drive this from windows task scheduler to run the script every so often.  How often do you want to check, every 5 minutes, every 1 minute, etc?

It could be done in BAT, but VBS would be a lot easier since it can handle date math quite a bit easier than a BAT script.  Would that work?

While I'm at it, if it were me, I'd probably use a directory monitoring tool for this rather than a script, likely be faster and easier to get working properly and dependably.

Here are some free ones you could look at:

http://www.brutaldev.com/page/Directory-Monitor.aspx
http://download.cnet.com/Directory-Monitor/3000-2248_4-10849871.html
http://www.contactplus.com/products/freestuff/monidir.htm
http://www.nodesoft.com/foldermonitor
http://www.gibinsoft.net/
http://leelusoft.blogspot.com/2010/07/watch-4-folder-22.html

And here are some paid ones:

http://www.watchdirectory.net/
http://www.coolutils.com/TotalFolderMonitor
http://www.networkautomation.com/
http://www.liquidmirror.com/alwayswatching.asp

~bp
ASKER CERTIFIED SOLUTION
Avatar of kollenh
kollenh
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cjb123

ASKER

I could see the powershell solution kollenh proposed working.  I changed one line of code though from $datediff.days to $datediff.totalminutes since I'm looking to see if there's a file in there that has existed for an unusual amount of time.  I would have accepted this, but the server I'm going to run this on doesn't have PS installed -__- hopefully I will give you your points tomorrow AM, I know you can't wait!
Oh yeah, sorry - I changed it to days to test on my system and forgot to change it back.  As billprew mentioned, you'll probably want to schedule it to run.  Also the directory monitoring tool is a good solution but with a script you have more control - you can monitor for additional parameters as the scope changes.  Which it usually does.

HTH
This example VBS will log all files older than 5 minutes. It will not log "old" files existing already when the script runs, only newly created ones. And you have to integrate the email stuff as shown above instead of logging. Using WMI Events, this solution has almost no overhead.
strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\cimv2")

Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("Select * From __InstanceCreationEvent Within 300 Where " _
    & "Targetinstance Isa 'CIM_DirectoryContainsFile' and " _
    & "TargetInstance.GroupComponent= " _
    & "'Win32_Directory.Name=""c:\\\\YourFolder\\\\ToMonitor""'")

Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
    Wscript.Echo objLatestEvent.TargetInstance.PartComponent
Loop

Open in new window

Avatar of cjb123

ASKER

I went with the PowerShell route, which worked well - small tweaks the script and I was up and running quickly.  Now I need to learn PowerShell since it looks like a great scripting language.