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
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
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.
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.
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.
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;
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With