SORT file in dir

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

SORT file in dir

Post by sal21 »

03-04-2021_nome file
10-12-2019__nome file
...
09-12-2017__nome file

I have this file in dir.

i need to insert the list of files in a combo box.

but before order files by date...

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SORT file in dir

Post by HansV »

Can't you name the files like 2021-02-26_nome file? Then they automatically would be sorted correctly .
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: SORT file in dir

Post by sal21 »

HansV wrote:
05 Mar 2021, 08:11
Can't you name the files like 2021-02-26_nome file? Then they automatically would be sorted correctly .
HUMMM...
problem i juts have a code to define the date in dd-mm-yyyy
and i use this data format in all part of my code:-(

in dir are approx 150 files

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SORT file in dir

Post by HansV »

Use code like this:

Code: Select all

    Const strFolder = "C:\MyDir\"
    Dim strFile As String
    Dim s As String
    Dim a() As String
    Dim b() As String
    Dim p() As String
    Dim i As Long
    Dim j As Long
    Dim tmp As String
    Dim n As Long
    strFile = Dir(strFolder & "*.*")
    Do While strFile <> ""
        n = n + 1
        ReDim Preserve a(1 To n)
        ReDim Preserve b(1 To n)
        s = Left(strFile, 10)
        p = Split(s, "-")
        s = p(2) & p(1) & p(0)
        a(n) = strFile
        b(n) = s
        strFile = Dir
    Loop
    For i = 1 To n - 1
        For j = i + 1 To n
            If b(j) < b(i) Then
                tmp = a(i)
                a(i) = a(j)
                a(j) = tmp
                tmp = b(i)
                b(i) = b(j)
                b(j) = tmp
            End If
        Next j
    Next i
    ComboBox1.List = a
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: SORT file in dir

Post by YasserKhalil »

Amazing my tutor.
Here's another similar approach

Code: Select all

Private Sub UserForm_Initialize()
    Dim v, fso As Object, oFolder As Object, arr As Object, oFile As Object, sFile As String, i As Long
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set oFolder = fso.GetFolder(ThisWorkbook.Path & "\TestFolder")
    Set arr = CreateObject("System.Collections.ArrayList")
    For Each oFile In oFolder.Files
        sFile = CStr(Split(oFile.Name, ".")(0))
        v = Split(Split(sFile, "_")(0), "-")
        arr.Add v(2) & v(1) & v(0) & Chr(2) & sFile
    Next oFile
    ReDim a(0 To arr.Count - 1)
    arr.Sort
    For i = 0 To arr.Count - 1
        a(i) = Split(arr(i), Chr(2))(1)
    Next i
    ComboBox1.List = a
End Sub

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SORT file in dir

Post by HansV »

@Yasser:

I tried code like that, but the line

Set arr = CreateObject("System.Collections.ArrayList")

causes an Automation Error on my PC.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: SORT file in dir

Post by YasserKhalil »

I think that is because of the NET framework 3.5 as from a long time, I got that error. Try installing NET framework 3.5

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SORT file in dir

Post by HansV »

I'd rather not install .NET Framework if it's not absolutely necessary.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: SORT file in dir

Post by YasserKhalil »

It is necessary for me as I am using selenium in VBA too.