Check for Most recent file in a 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
User avatar
plambrecht
Posts: 151
Joined: Wed May 19, 2004 8:11 am
Location: Belgium
Contact:

Check for Most recent file in a folder

Post by plambrecht »

Hi,

This test returns the fullpath&filename of the File that was last modified.
The filter of the files is a regular expression.

Code: Select all

' Created by Pieter Lambrecht
' Email: plambrecht AT icorda.be
' Version: 1,0
' Date 1/10/04
' Platform: Windows
' Requires vbScript v 5.6
' Parameters: "path" "expression"
'   path = path to search for files (for UNC of LFN paths, use ")
'   expression = Regular expression to define the filter
'       see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/reconintroductiontoregularexpressions.asp
'     eg: all files that end on LOG = LOG$
'         all files that start with TAPE = ^TAPE

Option Explicit
Dim FSO, fsFolder, objArgs, item, NewestFile, regEx, strPath

On Error Resume Next
Set objArgs = WScript.Arguments
If objArgs.count<>2 Then
  wscript.stdout.write "scriptres:BAD:Invalid number of parameters"
  wscript.quit
end if
strPath=objArgs(0)
If Right(strPath,1)<>"\" Then strPath=strPath & "\"

Set regEx = New RegExp

Set FSO = CreateObject("Scripting.FileSystemObject")
If Err<>0 Then
  wscript.stdout.write "scriptres:BAD:Unable to open Filesystem Object"
  wscript.quit
end if

Set fsFolder = fso.GetFolder(strPath)
If Err<>0 Then
  wscript.stdout.write "scriptres:BAD:Unable to open path '" & strPath & "'"
  wscript.quit
end if

Set NewestFile=Nothing
For each item in fsFolder.Files
  regEx.Pattern = objArgs(1)
  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

If NewestFile is Nothing then
  wscript.stdout.write "scriptres:BAD:No file found"
Else
  wscript.stdout.write "scriptres:OK:" & strPath & NewestFile.Name
End If

Hope it is usefull for someone..

Pieter
Post Reply