Find Latest File in a Directory

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Find Latest File in a Directory

Post by Marcus Tettmar » Thu Jun 09, 2005 6:59 am

Returns the latest file in a given directory:

VBSTART
Function NewestFile(Folder)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Folder)
dPrevDate = "0"
For Each oFile In oFolder.Files
If DateDiff("s", dPrevDate, oFile.DateLastModified) > 0 Then
sNewestFile = oFile.Path
dPrevDate = oFile.DateLastModified
End If
Next
NewestFile = sNewestFile
End Function
VBEND
VBEval>NewestFile("c:\windows"),filename

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts