Interesting code and beautiful result. Unfortunately today I will hardly have time to try to remake the code for ESP32 without a sd card, because there is no sd card. But anyway, I have a few questions:
Since I'm a beginner, I don't quite understand what a "struct" is. Could you briefly explain in your own words what it is and give a link where, in your opinion, it is popularly described?
In the so-called "original" and you use if(!root) and if(!root.isDirectory() but I do not quite understand what this gives, especially considering that these are two "if" in a row. I I tried to replace it with "else if" but apparently it doesn't work, or I'm checking it wrong, not understanding what exactly is checked by this.
Again, I do not understand the use of "return" ... Especially since it seemed to me that after "return" the function should generally be interrupted, returning the result. But it continues and code like File file = root.openNextFile();
Of course, I will try to redo the code for ESP32 and LittleFS today, but I'm not sure if I can do it
I tried to change it. For some reason, I'm swearing at this line. Here's an error:
error: format '%d' expects argument of type 'int', but argument 4 has type 'long unsigned int' [-Werror=format=]
Serial.printf("DIR : %-28s %10d\n",dirname, dirSize.local);
^~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~
cc1plus.exe: some warnings being treated as errors
For the sake of interest, I wrote 0 there, compiled and, in principle, it turned out not bad, except for the zeros in the directory size. But the total amount was calculated correctly this time.
-----------------------------------------------------
DIR : / 0
FILE: Arakona.jpg 65047
FILE: Egipt_08.jpg 18031
FILE: Test3.txt 0
FILE: fish10.jpg 18883
FILE: ignore.txt 0
FILE: index.htm 451
-----------------------------------------------------
DIR : /edit 0
FILE: index.htm 40877
-----------------------------------------------------
DIR : /kat01 0
-----------------------------------------------------
DIR : /kat1 0
FILE: kat11.txt 0
FILE: buhta.jpg 67589
-----------------------------------------------------
DIR : /kat1/kat11 0
-----------------------------------------------------
DIR : /mydir 0
FILE: Test2.txt 14
=====================================================
TOTAL SIZE: 210892
=====================================================
Other changes are also minimal. I just removed the restriction on the depth of scanning and work with maps. Why swears at a different data type, I can’t understand
I have never done this before, and to be honest, I still don’t know what kind of method it is to print information like this, but I found an example on the Internet with correcting a similar error and everything worked out. Did it like this:
-----------------------------------------------------
DIR : / 102412
FILE: Arakona.jpg 65047
FILE: Egipt_08.jpg 18031
FILE: Test3.txt 0
FILE: fish10.jpg 18883
FILE: ignore.txt 0
FILE: index.htm 451
-----------------------------------------------------
DIR : /edit 40877
FILE: index.htm 40877
-----------------------------------------------------
DIR : /kat01 0
-----------------------------------------------------
DIR : /kat1 67589
FILE: kat11.txt 0
FILE: buhta.jpg 67589
-----------------------------------------------------
DIR : /kat1/kat11 0
-----------------------------------------------------
DIR : /mydir 14
FILE: Test2.txt 14
=====================================================
TOTAL SIZE: 210892
=====================================================
Now I can think about combining the sketch so that it works for LittleFs and for SD cards. I don't want to do it under SPIFFS either. Even I managed to notice the problems that arise with it.
And I can try to combine the sketch for esp8266 and for esp32.
But those three questions that I voiced above still interest me.
Question 1: I have written an introduction how to use structs and from struct to class, will deploy this in the forum and provide the link when it is done.
I have found time again to work on Arduino. It's difficult to remember what I did before, but unfortunately, either I didn't save the working version or I never made a version for ESP8266. I'm now trying to recall and modify this example according to the specifications for ESP8266.
It looks like this:
14:8: error: no match for 'operator!' (operand type is 'fs::Dir')
There are other errors as well, but it seems that all the errors are related to this one. It's strange that a simple check like this is causing such an error. Out of curiosity, I tried a similar approach in one of the saved versions
void scan_dir(String path) {
Dir dir1 = LittleFS.openDir(path);
while (dir1.next()) {
if (!dir1.isDirectory()){
// if (dir1.isFile()){
Serial.print("File:\t");
Serial.print(path + dir1.fileName());
Serial.print("\tSize:\t");
Serial.println(dir1.fileSize());
}
if (dir1.isDirectory()) {
Serial.print("Dir:\t");
Serial.print(path+dir1.fileName()+"/");
Serial.print("\t\tSize:\t");
Serial.println(dirSize(path+dir1.fileName()+"/"));
scan_dir(path+dir1.fileName()+"/"); // Вызов рекурсии
}
}
}
And it works. Can you tell me where this error for evp8266 comes from?
The error message tells that the operator ! (not operator ) is not defined for the datatype of "fs::Dir".
In the function scan_dir() you do not use the ! operator with the variable dir1 (but with the function dir1.isDirectory() which returns a boolean value). That would explain why you do not get the same error message.
You may try to find out what datatype fs::Dir is, I guess it may be a pointer. Then it depends whether the function call
Dir root = LittleFS.openDir(path);
returns a simple zero or the NULL pointer when path is not existing ...
Instead of
if (!root) {
you can try these alternatives
if (root == 0) {
or
if (root == NULL) {
If you get an error message that NULL is not defined you may try to include
Unfortunately, this advice didn't help. I reworked the code differently. And frankly, I don't understand why for ESP32 there is no need to add an operator/modulator when opening a file. Additionally, I added a check for the existence of the directory. In my opinion, it makes the reason more understandable in case there is an error running the sketch. Also, I managed to modify the output of the directory name for ESP8266. Now it is complete. Honestly, it didn't work with a short name. And perhaps now it has become more visually clear. I haven't been able to do this for ESP32 yet. Nevertheless, after taking a closer look at the results of calculating the directory size, although it provides the correct result for the root directory, there is still an error when calculating the size of nested directories. Specifically, it only considers the overall size of files. But if there is a nested directory, that directory is not taken into account. Unfortunately, I haven't solved this problem yet.
Here is what the code looks like for ESP8266 now.
LittleFS Initialised OK
-----------------------------------------------------
DIR : / 92587
FILE: Egipt_08.zip 17688
FILE: favicon.ico 1150
FILE: fish10.7z 17557
FILE: index.htm 451
FILE: pins.png 55578
FILE: unknown.abrval 163
-----------------------------------------------------
DIR : edit 40877
FILE: index.htm 40877
-----------------------------------------------------
DIR : edit/kat11 0
-----------------------------------------------------
DIR : edit/kat11/kat21 0
-----------------------------------------------------
DIR : edit/kat12 0
-----------------------------------------------------
DIR : root_kat 0
=====================================================
TOTAL SIZE: 133464
=====================================================
In theory, the root directory should have been equal to 92587 + 40877 = 133464, which is the same as the overall result because it has a nested directory called "edit" that also contains files. The same problem exists in the ESP32 variant as well.
In your example, it's the same. The directory dir1 contains the directory dir2, but it only shows the sum of the files without including the dir2 directory.
I wouldn't say it's a big problem. At least it's not immediately noticeable. But it would be nice to have a more accurate picture. Perhaps in the format: total files + total directories.
Yes, I'm still thinking about it. Of course, I won't refuse a hint.))) In your opinion, is such a calculation of the total sum reasonable, or is it unnecessary?
Based on the sketch from post 41 I have made some little changes so that the sum is also printed, but sorted in the same way as the iteration comes back from "deeper" dir levels (recursive):
Should be not to complicated for you to identify the changes in lisDir() and add them to your sketch. It is mainly a boolean that is set to true if the recent directory has sub dirs and that leads to a print of the "SIZE INCL. SUBDIRS" in case it has. This size should represent all files on the directory level and below. It won't come at the beginning of the print (as it is not known at that time due to the recursive function!)
Example print:
DIR : /dir1/dir1.1 6574
FILE: 3.txt 3287
FILE: 4.txt 3287
=============== SIZE INCL. SUBDIRS ==================
DIR : /dir1 12931
=====================================================
------------------- LOCAL FILES ---------------------
DIR : /dir2 13
FILE: foo.txt 13
------------------- LOCAL FILES ---------------------
DIR : /dir2/dir2.1 6357
FILE: 2.txt 2770
FILE: 5.txt 3587
------------------- LOCAL FILES ---------------------
DIR : /dir2/dir2.1/dir2.1.1 6574
FILE: 3.txt 3287
FILE: 4.txt 3287
=============== SIZE INCL. SUBDIRS ==================
DIR : /dir2/dir2.1 12931
=====================================================
------------------- LOCAL FILES ---------------------
DIR : /dir2/dir2.2 6357
FILE: 2.txt 2770
FILE: 5.txt 3587
------------------- LOCAL FILES ---------------------
DIR : /dir2/dir2.2/dir2.2.1 6574
FILE: 3.txt 3287
FILE: 4.txt 3287
=============== SIZE INCL. SUBDIRS ==================
DIR : /dir2/dir2.2 12931
=====================================================
=============== SIZE INCL. SUBDIRS ==================
DIR : /dir2 25875
=====================================================
=============== SIZE INCL. SUBDIRS ==================
DIR : / 1090202
=====================================================
For ESP32 I have already redone it, I got the same result as yours. I hope that I understand the principle and can slightly change the output of information about subdirectories. And then I will do for ESP8266.
Are you using this sketch for ESP32? This is an example for ESP8266. They are slightly different. Usually people say in which line the error is, so that it is easier to understand where the problem is. Unfortunately, I don't know where in Platformio it shows the line that has an error.