【发布时间】:2018-02-11 23:29:40
【问题描述】:
下面的代码有助于查找文件夹中最早的文件日期,但我正在寻找可以帮助我查找文件夹中最新文件日期或最近文件日期的 VBA 代码。
Sub oldestdate()
Range("G10").Value = GetOldestFile("C:\Users\xxx\Downloads\My files")
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 NewestFile As String
Dim NewestDT 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
NewestDT = DateValue("1900-01-01")
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 > NewestDT Then
NewestFile = FoundFile
NewestDT = FileDT
End If
'// Get next file
FoundFile = Dir$
Loop
Set FS = Nothing
GetOldestFile = Format(NewestDT, "mm/dd/yyyy")
End Function
请告诉我如何在文件夹中找到最新的文件日期。
【问题讨论】:
-
检查一下,因为它似乎是您正在寻找的stackoverflow.com/questions/9205137/…