Sort files by date and delete oldest files

zliudr
Posts: 357
Joined: Thu Oct 03, 2019 5:15 am

Sort files by date and delete oldest files

Postby zliudr » Thu Jan 16, 2020 10:11 pm

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.

zliudr
Posts: 357
Joined: Thu Oct 03, 2019 5:15 am

Re: Sort files by date and delete oldest files

Postby zliudr » Thu Jan 16, 2020 10:27 pm

By the way, is there any "archive" attributes I can read/set in POSIX system? That would be great!

PeterR
Posts: 621
Joined: Mon Jun 04, 2018 2:47 pm

Re: Sort files by date and delete oldest files

Postby PeterR » Thu Jan 16, 2020 11:51 pm

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.
& I also believe that IDF CAN should be fixed.

zliudr
Posts: 357
Joined: Thu Oct 03, 2019 5:15 am

Re: Sort files by date and delete oldest files

Postby zliudr » Thu Jan 16, 2020 11:57 pm

Thanks PeterR. I'll post on stack overflow and provide a link Ina reply if I can find an answer.

PeterR
Posts: 621
Joined: Mon Jun 04, 2018 2:47 pm

Re: Sort files by date and delete oldest files

Postby PeterR » Fri Jan 17, 2020 12:34 am

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.
& I also believe that IDF CAN should be fixed.

zliudr
Posts: 357
Joined: Thu Oct 03, 2019 5:15 am

Re: Sort files by date and delete oldest files

Postby zliudr » Fri Jan 17, 2020 12:45 am

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.

PeterR
Posts: 621
Joined: Mon Jun 04, 2018 2:47 pm

Re: Sort files by date and delete oldest files

Postby PeterR » 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 also believe that IDF CAN should be fixed.

zliudr
Posts: 357
Joined: Thu Oct 03, 2019 5:15 am

Re: Sort files by date and delete oldest files

Postby zliudr » Fri Jan 17, 2020 5:38 am

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:

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 211 guests