Results 1 to 3 of 3

Thread: List of files in chronological order

  1. #1
    Senior Member
    Join Date
    Apr 2011
    Posts
    190
    Rep Power
    14

    List of files in chronological order

    Goodday

    I have a list of CSV files -- I know how to get the list of the file names into an array and then start to read each file. However I would like the order of the file names in my list to be in the order Oldest to Newest. Below is the code I use for the list of files- any help is much appreciated

    Code:
    Function GetFileList(FileSpec As String, FileArray() As Variant) As Variant
        'Where     FileSpec = PathToFiles & "\*.csv"
    
        Dim FileCount As Integer
        Dim FileName As String
        On Error GoTo NoFilesFound
        FileCount = 0
        FileName = Dir(FileSpec)
        If FileName = "" Then GoTo NoFilesFound
        Do While FileName <> ""
            FileCount = FileCount + 1
            ReDim Preserve FileArray(1 To FileCount)
            FileArray(FileCount) = FileName
            FileName = Dir()
        Loop
        GetFileList = FileArray
        Exit Function
    '   Error handler
    NoFilesFound:
        GetFileList = False
    End Function
    xl2007 - Windows 7
    xl hates the 255 number

  2. #2
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    Here is one way..

    Code:
    Function GetFileList(FileSpec As String) As Variant
        'Where     FileSpec = PathToFiles & "\*.csv"
        
        Dim i       As Long
        Dim Fldr    As String
        Dim Extn    As String
        Dim fl, f(), j As Long
        Dim wbk     As Workbook
        
        Fldr = Left$(FileSpec, InStrRev(FileSpec, "\"))
        Extn = Replace(FileSpec, Fldr, vbNullString)
        With CreateObject("scripting.filesystemobject").getfolder(Fldr)
            ReDim f(1 To .Files.Count, 1 To 2)
            For Each fl In .Files
                If fl.Name Like Extn Then
                    i = i + 1
                    f(i, 1) = fl.Name
                    f(i, 2) = fl.DateLastModified
                End If
            Next
            Set wbk = Workbooks.Add
            With wbk.Worksheets(1)
                .[a1].Resize(UBound(f, 1), 2) = f
                .[a1].Resize(UBound(f, 1), 2).Sort .Cells(1, 2), 1
                GetFileList = .[a1].Resize(UBound(f, 1))
            End With
            wbk.Close 0: Set wbk = Nothing
        End With
    End Function
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  3. #3
    Senior Member
    Join Date
    Jun 2012
    Posts
    337
    Rep Power
    13
    use the oneliner:

    Code:
    sub M_snb()
      sn=split(createobject("wscript.shell").exec("cmd /c dir G:\OF\*.csv /b /o-d").readall,vbcrlf)
    end sub
    The resulting array sn has been sorted by filedatetime.

Similar Threads

  1. Search Directories to List Files VBA
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 3
    Last Post: 04-15-2014, 08:22 PM
  2. Formula Following Order Of Calculation BODMAS
    By paul_pearson in forum Excel Help
    Replies: 7
    Last Post: 03-27-2013, 02:07 PM
  3. Unique Random Number In Ascending Order
    By marreco in forum Excel Help
    Replies: 8
    Last Post: 11-04-2012, 04:15 PM
  4. Get Name List of All Open Workbook Files
    By princ_wns in forum Excel Help
    Replies: 5
    Last Post: 04-07-2012, 12:18 PM
  5. List Of All Files In A Folder
    By Excel Fox in forum Excel Help
    Replies: 2
    Last Post: 10-27-2011, 09:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •