tags:

views:

453

answers:

3

I had tought that FindFirst found files in alphabetical order but recently i am finding that while this is true for the most part a few files are not in alphabetical order.

if FindFirst( AProgramPath, faAnyFile, ASearchRec ) = 0 then
repeat
  AFilename := ASearchRec.name;
until FindNext( ASearchRec ) <> 0;
FindClose( ASearchRec );

in a specific folder here with about 300 text files all but about 8-10 of the files are returned in the correct alphabetical order.

if findfirst does not return files in alphabetical order is there a method that can be used to sort the folders contents in alphabetical order so that findfirst returns files in alphabetical order?

Regards,

Bill

+7  A: 

No. See the documentation:

The FindFirstFile function opens a search handle and returns information about the first file that the file system finds with a name that matches the specified pattern. This may or may not be the first file or directory that appears in a directory-listing application (such as the dir command) when given the same file name string pattern. This is because FindFirstFile does no sorting of the search results. (emphasis added)

Sinan Ünür
+6  A: 

The FindFirst function does no sorting of the search results, but you can order the files using a TStringList.

Procedure GetOrderFiles();
var
 ListFiles : TStringList;
 result    : integer;
 ASearchRec: TSearchRec;
begin
 ListFiles         := TStringList.Create;
 try
   ListFiles.sorted  := True;  
   result        := findFirst(AProgramPath,faAnyFile,ASearchRec );
   while result = 0 do
   begin
    if (ASearchRec.name <> '.') and (ASearchRec.name <> '..') then
    ListFiles.add(ASearchRec.name);
    result:=FindNext(ASearchRec );
   end;
   findClose(ASearchRec );

   //process your files

   //....
   finally
    ListFiles.Clear;
    ListFiles.free;
 end;
end;
RRUZ
+2  A: 

FindFirstFile and FindNextFile return files in whatever order they appear in the directory. On an NTFS system, that's roughly alphabetical order. For something like FAT32, the order is fairly unpredictable (as long as no file is deleted, it's the order of creation, but when a file is deleted, the next file you create in that directory will re-use the slot left by the deleted file). For some remote file systems, the order is likely to be even less predictable.

It's possible to sort the items on disk for at least a few file systems (e.g. FAT/FAT32). In the DOS days, utilities to do so were fairly common, but under current systems they've mostly fallen out of favor because the Windows Explorer (and such) mostly sort files instead of just displaying them in the order provided by FindFirstFile/FindNextFile.

IMO, you should probably think very hard about doing the same. Sorting the data on the disk worked sort of well under DOS, because there wasn't much happening in the background most of the time, so if you sorted a directory it stayed sorted, at least for a while. Nowadays, a typical Windows box has 20+ processes running at startup, so even if you sort the directory, you can't depend on it staying sorted for any length of time.

Jerry Coffin
Thank-you... yes i was thinking about DOS days... good advise. the stringlist approach is interesting.
Bill Miller