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.