File System "ls" command doesn't list files by creation date

I have a Flexy 201 with firmware version 14.2s0. I am using the following function to delete the oldest file saved to an external SD card. The “ls” command is supposed to sort the contents of the directory based on file creation. For testing purposes I am creating 10 text files and then attempting to delete the first one created. The files are only listed in the correct order if they all created within the same hour. Once I have files that are created in different hours, the “ls” command no longer works correctly. The “ls” command seems to list the files at random. For example, if I create 10 files between 9:00am and 9:59am, they will be listed correctly with the newest file at index 0 and the oldest file at index 9. However, if I create a file at 10:01am, the list is no longer listed in the order of creation date.

Am I using the FS “ls” command wrong? Is there a special way I need to be creating files? Does the FS “ls” command work correctly with SD cards?

FUNCTION delete_file%($directory$, $NumOfFiles%)
file_count% = FS “count”, $directory$
PRINT "File Count "; file_count%
FOR $i% = 0 To file_count% - 1
ls$ = FS “ls”, $directory$, $i%
PRINT “File#”; $i%; " = "; ls$
NEXT $i%
IF (file_count% >= $NumOfFiles%) THEN
ls$ = FS “ls”, $directory$, file_count% - 1
//ls$ = FS “ls”, $directory$, 0
PRINT "Delete File = "; ls$
fileString$ = $directory$ + ls$
FS “rm”, fileString$
ENDIF
file_count% = FS “count”, $directory$
$delete_file% = file_count%
ENDFN

Hi @jkerr1234,

Do you mean that after 10am if you create a file the order of the index changes? If that’s the case could you let me know what the index value of the new file that’s created at 10am is? Also what kind of naming system are you using for these values? If the naming system is consistent you could delete files based off the name instead of the index value

Hi Tim, sorry for my delayed response. I was mistaken on when the order gets messed up. It happens almost immediately. I wrote a program to create a new file every five minutes. Each time I created a file, I printed the list of file using the FS “ls” command. You can see on the 3rd file created at 15:15 that the files are already out of order.

Here is the console screen from the EWON

Creating new Log_2020_10_22_15_06.txt file for 2020_10_22

File Count 1

File #0 = Log_2020_10_22_15_06.txt

Creating new Log_2020_10_22_15_10.txt file for 2020_10_22

File Count 2

File #0 = Log_2020_10_22_15_10.txt

File #1 = Log_2020_10_22_15_06.txt

Creating new Log_2020_10_22_15_15.txt file for 2020_10_22

File Count 3

File #0 = Log_2020_10_22_15_10.txt

File #1 = Log_2020_10_22_15_15.txt

File #2 = Log_2020_10_22_15_06.txt

Creating new Log_2020_10_22_15_20.txt file for 2020_10_22

File Count 4

File #0 = Log_2020_10_22_15_10.txt

File #1 = Log_2020_10_22_15_20.txt

File #2 = Log_2020_10_22_15_15.txt

File #3 = Log_2020_10_22_15_06.txt

Creating new Log_2020_10_22_15_25.txt file for 2020_10_22

File Count 5

File #0 = Log_2020_10_22_15_10.txt

File #1 = Log_2020_10_22_15_20.txt

File #2 = Log_2020_10_22_15_15.txt

File #3 = Log_2020_10_22_15_06.txt

File #4 = Log_2020_10_22_15_25.txt

You mentioned in your post that if I use a consistent naming system then I could delete files based off the name. I am using a consistent naming system. How do you recommend I delete based off the name? Is there an easy way to sort by name? Is there an easy way to convert the time/date in the file name to an integer value rather than a string?

I can’t really think of an easy way to do this, but here’s an idea that might work for you

You could make a string variable that grabs the date ie. year, month, day, and minute and a tag that watches for the change in an hour.

You could write a function does an onchange whenever it sees the value of the hour change, and then turns on a timer that will go through and delete a file every 5 minutes starting with the 00 minute all the way up to the 55 minute mark.