Age of newest file in folder

If you have information, script, utility, or idea that can be useful for HostMonitor community, you welcome to share information in this forum.
Post Reply
jsimotas
Posts: 5
Joined: Sat Jul 02, 2005 2:38 am

Age of newest file in folder

Post by jsimotas »

I copied (and added to) code from another user on this forum but now i can't find or give credit to the other post!

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
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

Great job! :-)
Thank you for your contribution!

Regards,
Max
Joe-Vis
Posts: 12
Joined: Sun Nov 24, 2002 6:00 pm

How do I use this

Post by Joe-Vis »

How do I use this
Joe-Vis
Posts: 12
Joined: Sun Nov 24, 2002 6:00 pm

Figured it out...

Post by Joe-Vis »

use as Shell Script....
If there is a different way please let me know.

Thanks
Joe
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Re: Figured it out...

Post by KS-Soft Europe »

Joe-Vis wrote:use as Shell Script....
Correct.
Joe-Vis wrote:If there is a different way please let me know.
This script is designed for "Shell Script" test method. Why don't you like "Shell script" test method? You just should create a new script in "Script Manager", copy foregoing script into "Script" area and select "cmd /c cscript /B /E:VBScript %Script% %Params%" from "Start cmd" dropdown.
http://www.ks-soft.net/hostmon.eng/mfra ... m#shellmng

Regards,
Max
Joe-Vis
Posts: 12
Joined: Sun Nov 24, 2002 6:00 pm

No problem with Shell Script method

Post by Joe-Vis »

Just was not sure if it was the right answer. It works great.
Post Reply