Check for any file older than X minutes in a directory

Below script could e.g. be used to monitor a directory where a fax is converted into a email.
If the directory is not empty within the "age_threshold" it should result in an alarm.

In custom.conf:

[Check for old files in C:\Path]
type=boolean
command=cscript /nologo C:\oldestfile.vbs C:\Path\

In oldestfile.vbs:


' This script processes the list of files in the directory 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 = 5000

Dim folder_path
folder_path = WScript.Arguments(0)

Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.getFolder(folder_path)
Dim old_file_not_found
old_file_found = 0
For Each file in f.Files
  Dim age
  age = DateDiff("n", file.DateLastModified, Now)
  If age > age_threshold Then
    old_file_found = 1
    Exit For
  End If
Next
WScript.Quit(old_file_found)

If you need to check different subfolders in the specified directory the below extension of the script can be used:

In oldestfile.vbs:

' 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 = 5000

Dim old_file_not_found
old_file_found = 0

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 Then
old_file_found = 1
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

scanFolder(WScript.Arguments(0))

WScript.Quit(old_file_found)