find最近的文件,返回一分钟内最后的x个文件

我所处的情况如下:我需要返回文件夹中最新文件的path。 我需要返回的文件数量是由“ numberOfFiles ”指定的,并且是从最近的降序。 例如,

File1.doc - Last modified at 8:42:00 PM File2.doc - Last modified at 8:43:00 PM File3.doc - Last modified at 8:44:00 PM 

numberOfFiles = 2,应该返回一个数组;

  File3.doc's path File2.doc's path 

这很多工作,与下面的代码。

 Option Explicit Sub test() Dim FileName As String Dim FileSpec As String Dim MostRecentFile As String Dim MostRecentDate As Date Dim Directory As String Dim resultArray() As String Dim groupedArray() As String Dim fileCounter As Integer Dim groupedArrayCounter As Integer Dim resultArrayCounter As Integer Dim i As Integer Dim numberOfFiles As Integer: numberOfFiles = 2 Directory = "C:\Test\" FileSpec = "File*.doc" If Right(Directory, 1) <> "\" Then Directory = Directory & "\" fileCounter = 0 FileName = Dir(Directory & FileSpec, 0) If FileName <> "" Then MostRecentFile = FileName MostRecentDate = FileDateTime(Directory & FileName) Do While FileName <> "" If FileDateTime(Directory & FileName) > MostRecentDate Then MostRecentFile = FileName MostRecentDate = FileDateTime(Directory & FileName) ReDim Preserve resultArray(fileCounter) resultArray(fileCounter) = FileName fileCounter = fileCounter + 1 End If FileName = Dir() Loop End If groupedArrayCounter = 0 resultArrayCounter = UBound(resultArray) ReDim groupedArray(numberOfFiles - 1) For i = numberOfFiles To 1 Step -1 groupedArray(groupedArrayCounter) = resultArray(resultArrayCounter) groupedArrayCounter = groupedArrayCounter + 1 resultArrayCounter = resultArrayCounter - 1 Next i MsgBox "Done" End Sub 

最后一个要求是最后一个要求,我不知道如何实现它。 虽然我需要能够返回numberOfFiles金额最近的文件(工作),我必须这样做,如果文件被修改在60秒或更less的对方(这也需要按降序从最近 – 在这个例子中,File3)。 例如;

  If file 2 is made within 60 seconds of file 3, add it to the final array If file 1 is made within 60 seconds of file 2, add it to the final array Etc until there are no more files or we have exceeded numberOfFiles 

帮助非常感谢

编辑:

我知道这可以通过使用DateDiff(“s”,var1,var2)来完成,我只是不完全确定这个逻辑如何从我的数组的uBound