Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to retrieve most recent created file from folder?

Status
Not open for further replies.

Sparko

IS-IT--Management
May 29, 2008
1
GB
Hi people,

Does anyone know how to obtain the latest created file from a folder containing a number of files and display its name.

I'm almost losing it!

Thanks alot.
 
Code:
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oNewestFile = Nothing
sFolder = "C:\Temp\"
If oFSO.FolderExists(sFolder) Then
  For Each oFile In oFSO.GetFolder(sFolder).Files
    If oNewestFile Is Nothing Then
      Set oNewestFile = oFile
    Else
      If oNewestFile.DateCreated < oFile.DateCreated Then
        Set oNewestFile = oFile
      End If
    End If
  Next
End If
If oNewestFile IsNot Nothing Then
  WScript.Echo oNewestFile.Name & " " & oNewestFile.DateCreated
End If
 
And another way to do it.

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")

FolderToScan = "C:\"

Set objFolder = objFSO.GetFolder(FolderToScan)

NewestFile = ""
NewestDate = #1/1/1970#

For Each objFile In objFolder.Files
    If objFile.DateLastModified > NewestDate Then
        NewestDate = objFile.DateLastModified
        NewestFile = objFile.Name
    End If
Next

WScript.Echo NewestFile

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top