Results 1 to 5 of 5

Thread: Sort Order of Dir() Function

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2009
    Posts
    88

    Sort Order of Dir() Function

    I have the following code to process all the text files found in the folder C:\Temp ...

    strFile = Dir("C:\Temp\*.txt")
    Do While strFile <> ""
    '<Code to process the file in strFile>
    strFile = Dir() 'Get next file in the folder
    Loop

    Let's say I have the files File1.txt, File2.txt, File3.txt, etc. in the Temp folder. What is the sort order that the files will come up with the series of Dir() function calls made by the program? Is there any way to control that sort order, say, if you want the files to be processed from earliest to latest file date/time?

    Any help would be much appreciated. Thanks in advance!

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,279

    Re: Sort Order of Dir() Function

    I am not aware of any sort options there, they show up in whatever order they do. you would likely have to write some code to do the sorting yourself if you want some order other than the order you get.

    You could do something like Explorer does and load them into a listview set to report mode and then sort by the filedate column

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,442

    Re: Sort Order of Dir() Function

    I am not sure what order they are returned, but you can implement a quicksort routine to sort by date, length, alphanumeric, etc.

  4. #4
    Fanatic Member
    Join Date
    Aug 2013
    Posts
    806

    Re: Sort Order of Dir() Function

    Short answer: the order is unpredictable and varies by file system. You must do any sorting manually.

    Long answer: this answer on StackOverflow describes how the FindFirstFile and FindNextFile API functions work. I assume Dir() wraps them in some capacity, so that information may be helpful.
    Check out PhotoDemon, a pro-grade photo editor written completely in VB6. (Full source available at GitHub.)

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2009
    Posts
    88

    Re: Sort Order of Dir() Function

    That's what I was afraid of. Well, I'll have to put the files in an array and sort them.

    Thanks everyone for your inputs and great info!

Posting Permissions

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



Click Here to Expand Forum to Full Width