Sort array of files in folder - by Date Modified

Formula11

Active Member
Joined
Mar 1, 2005
Messages
478
Office Version
  1. 365
Platform
  1. Windows
Is it possible to sort array of files in a folder by Date Modified, oldest to newest.
The output would be a sorted 2D array with date and filename, or two 1D arrays (date and corresponding filename).

The Quicksort methods seem to be for filenames.

Or is it possible to process files in Explorer based on date?

VBA Code:
Sub test()
    Dim BrowseWindow As FileDialog
    Dim folder_path As String
    Dim my_FSO As Object
    Dim the_folder As Object
    Dim file_item As Object
    Dim my_array() As Variant
    Dim i As Long
    'choose folder
    Set BrowseWindow = Application.FileDialog(msoFileDialogFolderPicker)
    With BrowseWindow
        .InitialFileName = ThisWorkbook.Path
        If .Show = -1 Then
            folder_path = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With
    'start
    Set my_FSO = CreateObject("Scripting.FileSystemObject")
    Set the_folder = my_FSO.GetFolder(folder_path)
    'loop
    i = 0
    For Each file_item In the_folder.Files
        ReDim Preserve my_array(i)
        my_array(i) = file_item.Name
        Debug.Print my_array(i)
        i = i + 1
    Next file_item
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Is it possible to sort array of files in a folder by Date Modified, oldest to newest.
The output would be a sorted 2D array with date and filename, or two 1D arrays (date and corresponding filename).

The Quicksort methods seem to be for filenames.

Or is it possible to process files in Explorer based on date?

VBA Code:
Sub test()
    Dim BrowseWindow As FileDialog
    Dim folder_path As String
    Dim my_FSO As Object
    Dim the_folder As Object
    Dim file_item As Object
    Dim my_array() As Variant
    Dim i As Long
    'choose folder
    Set BrowseWindow = Application.FileDialog(msoFileDialogFolderPicker)
    With BrowseWindow
        .InitialFileName = ThisWorkbook.Path
        If .Show = -1 Then
            folder_path = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With
    'start
    Set my_FSO = CreateObject("Scripting.FileSystemObject")
    Set the_folder = my_FSO.GetFolder(folder_path)
    'loop
    i = 0
    For Each file_item In the_folder.Files
        ReDim Preserve my_array(i)
        my_array(i) = file_item.Name
        Debug.Print my_array(i)
        i = i + 1
    Next file_item
End Sub
Try this.

It creates a worksheet named 'Files' If you already have a worksheet by this name then change the code.)

The worksheet is sorted and the array my_array populated with the sorted list of files or you to use as desired.

VBA Code:
Sub test_V2()
    Dim BrowseWindow As FileDialog
    Dim folder_path As String
    Dim my_FSO As Object
    Dim the_folder As Object
    Dim file_item As Object
    Dim my_array() As Variant
    Dim i As Long
    Dim arr() As String

    'choose folder
    Set BrowseWindow = Application.FileDialog(msoFileDialogFolderPicker)
    With BrowseWindow
        .InitialFileName = ThisWorkbook.Path
        If .Show = -1 Then
            folder_path = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With
    'start
    Set my_FSO = CreateObject("Scripting.FileSystemObject")
    Set the_folder = my_FSO.GetFolder(folder_path)
    ' loop
    
    If Evaluate("isref('" & "Files" & "'!A1)") Then
      Worksheets("Files").Cells.ClearContents
    Else
      Worksheets.Add after:=Sheets(Sheets.Count)
      ActiveSheet.Name = "Files"
    End If
    
    Worksheets("Files").Activate
    
    i = 1
    
    For Each file_item In the_folder.Files
      
      arr = Split(Str(file_item.DateLastModified), " ")
        
      Cells(i, 1).Resize(1, 3).Value = Array(file_item.Name, arr(0), arr(1))
      
      i = i + 1
    
    Next file_item
    
    Call subSortFiles
    
    my_array = ActiveSheet.Range("A1").CurrentRegion.Columns(1).Value

End Sub

Private Sub subSortFiles()
Dim lngLastRow As Long
  
  With ActiveSheet
  
    lngLastRow = .Cells(Rows.Count, "A").End(xlUp).Row

    .Sort.SortFields.Clear
    
    .Sort.SortFields.Add2 Key:=Range("B1:B" & lngLastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
    
    .Sort.SortFields.Add2 Key:=Range("C1:C" & lngLastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
       
    With .Sort
      .SetRange Range("A1:C" & lngLastRow)
      .Header = xlNo
      .Apply
    End With

  End With
  
End Sub
 
Upvote 0
Solution
try
Rich (BB code):
Sub test()
    Dim BrowseWindow As FileDialog
    Dim folder_path As String
    Dim my_FSO As Object
    Dim the_folder As Object
    Dim file_item As Object
    Dim my_array() As Variant
    Dim i As Long
    'choose folder
    Set BrowseWindow = Application.FileDialog(msoFileDialogFolderPicker)
    With BrowseWindow
        .InitialFileName = ThisWorkbook.Path
        If .Show = -1 Then
            folder_path = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With
    'start
    Set my_FSO = CreateObject("Scripting.FileSystemObject")
    Set the_folder = my_FSO.GetFolder(folder_path)
    'loop
    Dim d
    i = 0
    For Each file_item In the_folder.Files
        ReDim Preserve my_array(2, i)
        d = Split(file_item.DateLastModified)
        d(0) = CDbl(DateValue(d(0))): d(1) = CDbl(TimeValue(d(1)))
        my_array(0, i) = file_item.Name
        my_array(1, i) = file_item.DateLastModified
        my_array(2, i) = d(0) + d(1)
        Debug.Print my_array(0, i)
        i = i + 1
    Next file_item
    my_array = WorksheetFunction.SortBy(my_array, Application.Index(my_array, 3, 0), 1)
    For i = LBound(my_array) To UBound(my_array, 2)
        Debug.Print my_array(1, i), my_array(2, i)
    Next
End Sub
 
Upvote 0
Fuji, are you able to explain what each of these means
1735367274494.png
 
Upvote 0
Code:
        ReDim Preserve my_array(2, i)
my_array is 3 rows and i columns.
SortBy function should have base array to be sorted, i.e. my_Array
and 2nd array, sort key array, i.e Aapplication.Index(my_array,3,0), that is, whole 3rd row of my_array since 3rd row holds the serial date.

You can't sort the data properly when the date is string.

HTH.
 
Upvote 0

Forum statistics

Threads
1,226,113
Messages
6,189,048
Members
453,522
Latest member
Seeker2025

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top