1

私がいる状況は次のとおりです。フォルダー内の最新のファイルのパスを返す必要があります。返す必要があるファイルの数は「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

土壇場で最後の要件が 1 つ課せられましたが、どうすればそれを達成できるかわかりません。numberOfFilesの量の最新のファイルを返すことができる必要がありますが(これは機能します)、ファイルが互いに 60 秒以内に変更された場合にのみそうする必要があります (これも、ファイルから降順で行う必要があります)。最新 - この例では 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 から始まる降順でロジックがどのように機能するかは完全にはわかりません

4

0 に答える 0