Newest File in directory


/ Published in: Visual Basic
Save to your folder(s)

retrieves newest edited file in specified directory (optional file extension)


Copy this code and paste it in your HTML
  1. Public Function NewestFile(Directory As String, FileSpec As String) As String
  2. ' Returns the name of the most recent file in a Directory
  3. ' That matches the FileSpec (e.g., "*.xls").
  4. ' Returns an empty string if the directory does not exist or
  5. ' it contains no matching files
  6. ' all files : "*.*"
  7. Dim FileName As String
  8. Dim MostRecentFile As String
  9. Dim MostRecentDate As Date
  10. If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
  11. FileName = Dir(Directory & FileSpec, 0)
  12. If FileName <> "" Then
  13. MostRecentFile = FileName
  14. MostRecentDate = FileDateTime(Directory & FileName)
  15. Do While FileName <> ""
  16. If FileDateTime(Directory & FileName) > MostRecentDate Then
  17. MostRecentFile = FileName
  18. MostRecentDate = FileDateTime(Directory & FileName)
  19. End If
  20. FileName = Dir
  21. Loop
  22. End If
  23. NewestFile = Directory & MostRecentFile
  24. End Function

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.