El_Diabolo
New Member
- Joined
- Nov 23, 2013
- Messages
- 4
Hi everyone.
I want to find the most recently created file in a folder. I came across a previous post on this subject and I have taken the liberty of posting its code below (I hope this doesn't break forum rules). My problem is that I don't understand how it works. What is "fileDate"? If both "file.DateCreated" and "fileDate" belong to the same file then how can the former ever exceed the latter? Sorry to be thick, but I just don't get it. I would be very grateful if someone could explain it to me. Many Thanks. Here is the code:
I want to find the most recently created file in a folder. I came across a previous post on this subject and I have taken the liberty of posting its code below (I hope this doesn't break forum rules). My problem is that I don't understand how it works. What is "fileDate"? If both "file.DateCreated" and "fileDate" belong to the same file then how can the former ever exceed the latter? Sorry to be thick, but I just don't get it. I would be very grateful if someone could explain it to me. Many Thanks. Here is the code:
Code:
Sub mostRecentCreatedFile()
'Folder path you want to search
Dim strFolderPath As String: strFolderPath = "C:\Meeting 2013"
Dim fileSystem As Object: Set fileSystem = CreateObject("Scripting.FileSystemObject")
Dim folder As Object: Set folder = fileSystem.GetFolder(strFolderPath)
Dim file As Object
Dim strFilename As String
Dim fileDate As Date
Dim ShowDate As Date
'Loop through all files in directory
For Each file In folder.Files
'Find newest file that is also an HTML or XML (Report/Excel)
'If file.DateCreated > fileDate And (file.Type = "HTML Document" Or file.Type = "XML") Then
'Finds newest file of any type
If file.DateCreated > fileDate Then
fileDate = file.DateCreated
strFilename = file.Name
ShowDate = file.DateCreated
End If
Next file 'Displays last created file name
MsgBox strFilename & " " & fileDate & " " & ShowDate
End Sub
Sub
Last edited: