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

Check for Most recent file in a folder

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



Joined: 19 May 2004
Posts: 151
Location: Belgium

PostPosted: Fri Oct 01, 2004 1:05 pm    Post subject: Check for Most recent file in a folder Reply with quote

Hi,

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

Code:
' 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
Back to top
View user's profile Send private message Visit poster's website
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