KS-Soft. Network Management Solutions
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister    ProfileProfile    Log inLog in 

Age of newest file in folder

 
Post new topic   Reply to topic    KS-Soft Forum Index -> Library
View previous topic :: View next topic  
Author Message
jsimotas



Joined: 02 Jul 2005
Posts: 5

PostPosted: Thu Nov 22, 2007 11:22 am    Post subject: Age of newest file in folder Reply with quote

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:

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
Back to top
View user's profile Send private message
KS-Soft Europe



Joined: 16 May 2006
Posts: 2832

PostPosted: Fri Nov 23, 2007 4:23 am    Post subject: Reply with quote

Great job!
Thank you for your contribution!

Regards,
Max
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Joe-Vis



Joined: 24 Nov 2002
Posts: 12

PostPosted: Mon Mar 10, 2008 10:47 pm    Post subject: How do I use this Reply with quote

How do I use this
Back to top
View user's profile Send private message
Joe-Vis



Joined: 24 Nov 2002
Posts: 12

PostPosted: Mon Mar 10, 2008 11:15 pm    Post subject: Figured it out... Reply with quote

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

Thanks
Joe
Back to top
View user's profile Send private message
KS-Soft Europe



Joined: 16 May 2006
Posts: 2832

PostPosted: Tue Mar 11, 2008 2:35 am    Post subject: Re: Figured it out... Reply with quote

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/mframe.htm#tests.htm#shellmng

Regards,
Max
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Joe-Vis



Joined: 24 Nov 2002
Posts: 12

PostPosted: Tue Mar 11, 2008 12:35 pm    Post subject: No problem with Shell Script method Reply with quote

Just was not sure if it was the right answer. It works great.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    KS-Soft Forum Index -> Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group

KS-Soft Forum Index