Page 1 of 1

Sort files by date and delete oldest files

Posted: Thu Jan 16, 2020 10:11 pm
by zliudr
I'm trying to write a maintenance routine to delete old files from an sd card when total file size is above a threshold.
I'm thinking about how to sort last-modified time and here is what I've come up with:
1. Use opendir and readdir() to read struct for each file in the folder.
2. With the file name, call stat() to get file size and last-modified time. Save name, size, last-mod in a list. Sum up the file sizes while enumerating files. If it exceeds the limit, proceed to maintenance after enumeration is done, otherwise quit.

Sort the list according to last-mod. Start deleting files from the sorted list of names and decrement total file size after each deletion, until the size falls below threshold.

I am wondering if there are any standard libraries to read sorted file names by last-modified and whether my above description is sound. Thanks.

Re: Sort files by date and delete oldest files

Posted: Thu Jan 16, 2020 10:27 pm
by zliudr
By the way, is there any "archive" attributes I can read/set in POSIX system? That would be great!

Re: Sort files by date and delete oldest files

Posted: Thu Jan 16, 2020 11:51 pm
by PeterR
A POSIX question and so not ESP32 specific (I am sure that ESP comply with POSIX). You will therefore probably get better help on StackOverflow etc because of user volumes. I would be suprised if there is not a posted solution to your question.
EDIT: Plus StackOverflow etc allow votes which simplifies your review.

Re: Sort files by date and delete oldest files

Posted: Thu Jan 16, 2020 11:57 pm
by zliudr
Thanks PeterR. I'll post on stack overflow and provide a link Ina reply if I can find an answer.

Re: Sort files by date and delete oldest files

Posted: Fri Jan 17, 2020 12:34 am
by PeterR
PS Search first and show some attempt at solution with code else Stack will tend to roast you (or use a burner account!) But I think your's is a generic problem that (following an inteligent search) will have a well defined/accepted solution.

Re: Sort files by date and delete oldest files

Posted: Fri Jan 17, 2020 12:45 am
by zliudr
Thanks for you advice. I read a few hits from their auto-gen similar questions. My sense is that what I described was how it's done in all the posts I found (all different programming languages) but wonder if there's a better way.

Re: Sort files by date and delete oldest files

Posted: Fri Jan 17, 2020 1:10 am
by PeterR
There are always better ways. Its great that you want to push forward. Delivery and improvement need to be held in balence though.
IMHO the modern agile approaches are to get 'working' (test/functional) and then you may reflect on better structure (refactor etc).
The solutions you read may not actually work well but you won't know that until you apply test cases. Empiric, test driven, then you learn IMHO.

Re: Sort files by date and delete oldest files

Posted: Fri Jan 17, 2020 5:38 am
by zliudr
PeterR wrote:
Fri Jan 17, 2020 1:10 am
There are always better ways. Its great that you want to push forward. Delivery and improvement need to be held in balence though.
IMHO the modern agile approaches are to get 'working' (test/functional) and then you may reflect on better structure (refactor etc).
The solutions you read may not actually work well but you won't know that until you apply test cases. Empiric, test driven, then you learn IMHO.
I agree. Trial and error. It seems that stackoverflow is overflowing with snobs anyway. Those who can't handle a single question without acting superior should just code and keep their mouths shut. :lol: