We Recommend

Visual Basic 2008 Programmer's Reference Visual Basic 2008 Programmer's Reference
Visual Basic Orcas Programmer's Reference is a language tutorial and a reference guide to the upcoming Orcas release of Visual Basic. The tutorial provides basic material suitable for beginners but also includes in-depth content for more advanced developers.


Posted By

qrist0ph on 08/08/08


Tagged

sysadmin vbs


Versions (?)


VBS Find Newest File


Published in: Visual Basic 


  1. msgbox GetNewestFile("c:\tmp")
  2.  
  3. Function GetNewestFile(ByVal sPath)
  4.  
  5. Set oFSO = CreateObject("Scripting.FileSystemObject")
  6. sNewestFile = Null ' init value
  7.  
  8. Set oFSO = CreateObject("Scripting.FileSystemObject")
  9. Set oFolder = oFSO.GetFolder(sPath)
  10. Set oFiles = oFolder.Files
  11.  
  12. ' enumerate the files in the folder, finding the newest file
  13. For Each oFile In oFiles
  14. On Error Resume Next
  15. If IsNull(sNewestFile) Then
  16. sNewestFile = oFile.Path
  17. dPrevDate = oFile.DateLastModified
  18. Elseif dPrevDate < oFile.DateLastModified Then
  19. sNewestFile = oFile.Path
  20. End If
  21. On Error Goto 0
  22. Next
  23.  
  24. If IsNull(sNewestFile) Then sNewestFile = ""
  25.  
  26. GetNewestFile = sNewestFile
  27.  
  28. End Function

Report this snippet 

You need to login to post a comment.