Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is FindFirst supposed to return found files in Alphabetical order?

Tags:

delphi

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

like image 444
Bill Miller Avatar asked Oct 20 '09 16:10

Bill Miller


People also ask

What is the return type of findfirst() method in stream?

The findFirst () method returns an Optional describing the first element of the given stream if Stream is non-empty, or an empty Optional if the stream is empty. 1. Stream findFirst () Method

Can findfirstfile sort the files it returns?

As everyone has pointed out, FindFirstFile () does not and can not sort the files it returns. It operates at a fairly low level, and returns files in an order that relates to file system's natural order of directory entries in a directory.

What is the use of findfirst () method in Java?

This method returns first element satisfying the intermediate operations. Example 1 : findFirst() function on Stream of Integers. // Java code for Stream findFirst() // which returns an Optional describing. // the first element of this stream, or. // an empty Optional if the stream is empty.

What happens if the findfirstfile function fails?

If the function fails because no matching files can be found, the GetLastError function returns ERROR_FILE_NOT_FOUND. 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.


3 Answers

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 );
   try
     while result = 0 do
     begin
      if (ASearchRec.name <> '.') and (ASearchRec.name <> '..') then
      ListFiles.add(ASearchRec.name);
      result:=FindNext(ASearchRec );
     end;
   finally 
     FindClose(ASearchRec );
   end;  

   //process your files

   //....
 finally
    ListFiles.free;
 end;
end;
like image 183
RRUZ Avatar answered Oct 27 '22 08:10

RRUZ


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)

like image 36
Sinan Ünür Avatar answered Oct 27 '22 08:10

Sinan Ünür


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.

like image 30
Jerry Coffin Avatar answered Oct 27 '22 09:10

Jerry Coffin