Anyways, I use this vbscript to check the age (in minutes) of the newest file in a folder. In my case a process I monitor is supposed to export a daily file so I use:
testname "c:\myfolder" 1440
The only weird thing is that the optional file filter has to be in regular expression format like:
testname "c:\myfolder" 1440 ".txt$" (for files matching *.txt)
NewestFile.vbs:
Code: Select all
Option Explicit
Call main
Sub main
On Error Resume Next
Dim FSO, fsFolder, objArgs, item, NewestFile, regEx, strPath
Dim intAgeMinsMax, intAgeMins, strPattern
Set objArgs = WScript.Arguments
If (objArgs.count<2) Or (objArgs.count>3) Then
wscript.stdout.write "scriptres:BAD:Invalid number of parameters 2 or 3 (you supplied "& objArgs.count & ") (e.g. c:\temp 30 .txt$)" & vbCrLf
wscript.stdout.write " ---------------------------------------------------"& vbCrLf
wscript.stdout.write " newestfile.vbs ""path"" MaxAgeMins ""[expression]"" "& vbCrLf
wscript.stdout.write " ---------------------------------------------------"& vbCrLf
wscript.stdout.write " path = path to search for files (for UNC of LFN paths, use "") " & vbCrLf
wscript.stdout.write " maxagemins = 1440 (1 day)" & vbCrLf
wscript.stdout.write " expression [optional]= Regular expression to define a filter " & vbCrLf
wscript.stdout.write " eg: all files that end with LOG = LOG$ " & vbCrLf
wscript.stdout.write " all files that start with TAPE = ^TAPE " & vbCrLf
wscript.quit
end If
'------------------- params
strPath=objArgs(0)
intAgeMinsMax=objArgs(1)
If objArgs.count=3 Then
strPattern=objArgs(2)
Else
strPattern=""
End If
'------------------- age is number?
If Not IsNumeric(intAgeMinsMax) Then
wscript.stdout.write "scriptres:BAD:MaxAgeMins=" & intAgeMinsMax & " (this is not a valid number)"
wscript.quit
End If
'------------------- new object
Set FSO = CreateObject("Scripting.FileSystemObject")
If Err<>0 Then
wscript.stdout.write "scriptres:BAD:Unable to open Filesystem Object"
wscript.quit
End If
'------------------- folder listing
If Right(strPath,1)<>"\" Then strPath=strPath & "\"
Set fsFolder = fso.GetFolder(strPath)
If Err<>0 Then
wscript.stdout.write "scriptres:BAD:Unable to open path '" & strPath & "'"
wscript.quit
End If
'------------------- search through results
Set regEx = New RegExp
Set NewestFile=Nothing
For each item in fsFolder.Files
regEx.Pattern = strPattern
regEx.IgnoreCase = True
If regEx.Test(Item.Name) Then
If NewestFile is Nothing Then Set NewestFile=Item
If Item.DateLastModified>NewestFile.DateLastModified then Set NewestFile=Item
End If
Next
'------------------- results
If NewestFile is Nothing then
wscript.stdout.write "scriptres:BAD:No file found"
Else
intAgeMins = DateDiff("n", NewestFile.DateLastModified, Date+Time)
If CLng(intAgeMins) >= CLng(intAgeMinsMax) Then
wscript.stdout.write "scriptres:BAD:" & intAgeMins & " (" & NewestFile.Name & ")"
Else
wscript.stdout.write "scriptres:OK:"& intAgeMins & " (" & NewestFile.Name & ")"
End If
End If
'---------------- debug
'wscript.stdout.write "----------------------------------------" & vbCrLf
'wscript.stdout.write "Path=" & strPath & vbCrLf
'wscript.stdout.write "AgeMins=" & intAgeMins & vbCrLf
'wscript.stdout.write "Pattern=" & strPattern & vbCrLf
'wscript.stdout.write "----------------------------------------" & vbCrLf
End Sub