Sort array of files in folder - by Date Modified

Formula11

Active Member
Joined
Mar 1, 2005
Messages
475
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

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
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

Forum statistics

Threads
1,225,073
Messages
6,182,702
Members
453,132
Latest member
nsnodgrass73

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