What is the SD file read sort order ?

Hi, I am doing a project using the SD library, and require to read some files and assign it with a fixed serial_no.
The serial no is based on the file read order :

dataFile = SD.open(dir) ;

while (true)
{
File entry = dataFile.openNextFile();

if (entry)
{
tmp_file=entry.name() ;
if (serial_no<127) serial_no=serial_no+1 ; // attach a serial no to the file

// further processing .......

}

entry.close();
}
dataFile.close() ;
}

After some experiments, I found that the file read order does not have a particular sort order :

  • it does not sort by alphabat
  • it does not sort by modified time

Whenever I update the files in the folder (add file or modify file cotent), the list order will be changed.

Even when I delete the folder, recreate it and copy the same files to it, the list order will be different in an unexpected way !!!

Is there anyway to solve this problem ?

Thanks

I am using a SD card too. It seems to me that an order depends on an order stored in FAT. When I use Windows XP to copy my files to a SD card from a HDD I use a Windows sort function using Name as a mark. This way they are sorted alphabetically when I read them using Arduino sketch displaying root.
A sketch name is "cardinfo" as I remember.

Thanks for your help.

I have tested that when a folder is created new in the SD, and the file in the computer is sorted and then copy to the SD, the files in the SD folder will be listed in the sorted order.

But the problem is, when I update a file in the computer and overwrite it to the SD folder, the list order of this file will be changed.

I even tested to delete the SD folder, then re-create it and sort copy the files into the SD. But the resulting file list in the SD is totally different from the sorted order !!!

I have experimented creating and renaming folders to see the file list order in the SD, and it seems that only when a folder is created in the very first time, you can get a sorted order file listing. If the folder is created before or the file in the folder is updated, the file sorting order will be unpredictable.

KK959:
If the folder is created before or the file in the folder is updated, the file sorting order will be unpredictable.

I can't see why this would be a problem. Assuming of course that you have an onboard RTC, all you need do is name the file with the date, and Windows will sort it. I understand there are SD card libraries that will timestamp files. I guess you don't even need a clock, naming the files with consecutive numbers would suffice.

Thanks for your comment. I think you misunderstand my problem.

My goal is not to get sorted files in the SD card and copy it back to PC.

My purpose is to have a folder of preset files with a fixed serial reference to each file. I can give any name to the preset file, once the preset file name is in the foler and fixed to a serial no, my porgram can read it by refering to their serial no without knowing it's name.

If I update the file in the PC and copy it to the folder and the serial order is changed, it is a problem.