LittleFS list directory in alphabetical order

arjen1
Posts: 16
Joined: Sun Aug 06, 2023 4:48 pm

LittleFS list directory in alphabetical order

Postby arjen1 » Fri Mar 22, 2024 12:04 pm

Dear forum

Got an application in which I need to list the files and directories now and then.
Currently I am using a recursive function that iterates over the files and directories in the order LittlFS finds these. However, I want it to be a bit more fancy like so:

/dir1
/dir2
/dir...
afile1
bfile2
cfile3
..
zfile

In words: The list should first list all the directories and then the files in the current directory in alphabetical order.

I searched for such a function but came up empty.
I guess this question both applies to ESP8266 as ESP32.
Thanks...

User avatar
Inq720
Posts: 37
Joined: Fri Dec 22, 2023 1:36 pm
Location: North Carolina, USA
Contact:

Re: LittleFS list directory in alphabetical order

Postby Inq720 » Fri Mar 22, 2024 1:54 pm

Something that simple is typically not in a library. Use strcmp() function to compare two strings and find out which one comes first alphabetically. The reason it is not typically found on an MCU is memory requirements. If you have a huge number of files/folders, you have to bring all those strings into memory to see which one comes first and thus print it out. You might exceed the available memory for whatever is more important for the processor to be doing (beside sorting files). You could write the algorithm to read through the files (and not store them), but that would entail reading them ~n^2 times... and thus take a while on large file systems. If you still want to do it, you'll find plenty of hits with a simple Google search - https://www.google.com/search?q=C%2B%2B ... e&ie=UTF-8

I always set up my ESP8266 projects to be web servers and I send the list of files to a browser client and sort them using JavaScript on the browser where you might have a multi-GHz and multi-processor and multi-GB just idling along. Image

arjen1
Posts: 16
Joined: Sun Aug 06, 2023 4:48 pm

Re: LittleFS list directory in alphabetical order

Postby arjen1 » Fri Mar 22, 2024 2:52 pm

Thank you for your reply.

The setup you describe and the image you sent is pretty much what I am trying to implement. Albeit that I do not want the entire directory layout but only the files and dirs in the current directory. Pressing a listed directory would bring the user in that directory with the files in that directory.
I had a hard time figuring things out with espasyncwebserver. What I want is a directory listing and then the options to delete the file,rename the file, add a directory, download a file, upload a file, well the things one might do when handling files. I now use an XMLHttpRequest, and the server (esp8266) then returns the HTML code as a response. But, as per your suggestion, it might be more practical to send just the dirs/files and let the client sort stuff. I read that a JSON protocol back and fourth is the way to go? Not very familiar with that, but I can study it.

Could you post or send me a sort of example on how to achieve that? Both the server as the client part? I am not a pro programmer but I am trying to learn.

Or just the entire setup as in your example image you posted. I could go from there. Got it on GitHub somewhere?

Thanks

User avatar
Inq720
Posts: 37
Joined: Fri Dec 22, 2023 1:36 pm
Location: North Carolina, USA
Contact:

Re: LittleFS list directory in alphabetical order

Postby Inq720 » Fri Mar 22, 2024 3:30 pm

arjen1 wrote:
Fri Mar 22, 2024 2:52 pm
But, as per your suggestion, it might be more practical to send just the dirs/files and let the client sort stuff. I read that a JSON protocol back and fourth is the way to go? Not very familiar with that, but I can study it.
If you are trying to make a full featured File Manager, it is certainly the way to go. Send the file info down once and the user can sort by alpha, date, size and you don't have to re-query the ESP "Server"... just re-order/hide/show the files you want based on the user's input.
arjen1 wrote:
Fri Mar 22, 2024 2:52 pm
Could you post or send me a sort of example on how to achieve that? Both the server as the client part? I am not a pro programmer but I am trying to learn.
Or just the entire setup as in your example image you posted. I could go from there. Got it on GitHub somewhere?
Thanks
If you are using the Arduino IDE, it is a free library in the Library Manager, however, the library is not Open Source and it only is available in the Arduino IDE and only on the ESP8266. I provide the library (and free tech support) for people that want to get to the end project and not mess with all the boiler plate for a Web Server, WiFi Manager, File Manager, and OTA Manager. It also uses fast communications via Web Sockets. It does use JSON for intercommunications. For developers (like you) that want to roll their own, it is not a good candidate. You can only see code on the client side as JavaScript is readily exported.

Arduino Library: Search for InqPortal
There is an extensive website for server and client side manuals, examples and tutorials. Here is the the 5 minute tutorial to illustrate the basics and has links to all the other pages on the website - https://inqonthat.com/inqportal-the-three-line-promise/

Who is online

Users browsing this forum: No registered users and 44 guests