How could I modify this VBScript to return only the newest file’s name and Last Modified date? Currently it returns anything modified in the last 24 hours. I want to look for the most recent file only. I borrowed this from StackOverflow, not yet a VBScript wizard.
option explicit
dim fileSystem, folder, file
dim path
path = "C:\test"
Set fileSystem = CreateObject("Scripting.FileSystemObject")
Set folder = fileSystem.GetFolder(path)
for each file in folder.Files
if file.DateLastModified > dateadd("h", -24, Now) then
'whatever you want to do to process'
WScript.Echo file.Name & " last modified at " & file.DateLastModified
end if
next
For working with files on
VBScriptit is recommended you use theFileSystemObject.The
FileSystemObjecthas the following feature’s that help you solve your problem:FileSystemObject.GetFolder– Returns a Folder object corresponding to the folder in a specified path.Folder.Files– Returns a Files collection consisting of all File objects contained in the specified folder.File.DateLastModified– Returns the date and time that the specified file or folder was last modified.To demonstrate, I have supplied an implementation of
GetRecentFile(andGetRecentFolder) which scans the supplied input path and determines the most recent file by finding the file that has the greatestDateLastModifiedproperty.References: