以下代码显示文件目录中最旧的文件名,但我有兴趣知道最早的文件日期,但不知道最旧的文件名。
Sub oldestdate()
Range("G10").Value = GetOldestFile("xxxxx\yyyy\gggggg\uuuuuu")
End Sub
Public Function GetOldestFile(ByVal FileFolder As String, _
Optional ByVal FileMask As String = "*.*", _
Optional ByVal FullName As Boolean = True) As String
Dim FoundFile As String
Dim FileDT As Date
Dim OldestFile As String
Dim OldestDT As Date
Dim FS As Object
'// Get rid of any terminating '\' just to get to a known state
If Right(Trim(FileFolder), 1) = "\" Then
FileFolder = Left(FileFolder, Len(Trim(FileFolder)) - 1)
End If
'// Get First file found in described folder
FoundFile = Dir$(FileFolder & "\" & FileMask)
'// Default return date
OldestDT = Now
Set FS = CreateObject("Scripting.FileSystemObject")
'// Loop through the rest of the files in that folder
Do Until FoundFile = ""
FileDT = FS.GetFile(FileFolder & "\" & FoundFile).DateCreated
'// Compare Current File datetime with oldest found
If FileDT < OldestDT Then
OldestFile = FoundFile
OldestDT = FileDT
End If
'// Get next file
FoundFile = Dir$
Loop
Set FS = Nothing
GetOldestFile = Format(OldestDT, "mm/dd/yyyy")
End Function
请检查代码,如果您有任何问题或意见,请与我们联系。