Summing the file sizes in SD.h

Hi,

I am using an SD card to log data. I need to check check how much free space there is. I know I can use LS_SIZE on the volume to list all the files and their respective sizes. My question is, how would I grab each of the individual file sizes so I can sum them. When listing all the files and sizes, the result looks like this:

File1 285
File2 466
File3 6372

And so on. Obviously file name on the left and size on the right.

Any help would be very much appreciated.

Take a look at the listfiles example. In that sketch the entry.size() returns the size of the individual file. Summing that up shouldn’t be a problem.

That makes sense. How would you read in each file name? One catch I have is that the files will be generated and named as a time stamp. So in effect, I won’t know the file name up front to program in. If using something like LS, can I somehow read each of the file names in as a variable that I can then run entry.size() upon?

Thank you for your help!

Have you looked at the program? It does list the files on the SD card.

I don’t know what your problem is.

Pylon,

I have read the listfiles example. Thanks for pointing that out, it makes a lot more sense now.

New issue:

I am trying to incorporate the listfiles example into my code as its own function.

DataLogger_4-24-21.ino (9.7 KB)

I cannot for the life of me figure out why doing so breaks my code. I have tried to keep as much of the listfiles example code the same as possible just for initial debugging purposes. If I take the “printDirectory” function out of my code and run it separately, it works as it should. Additionally, with it out of my code, the rest of my code works as it should. When I put it back in my code, the function does not print anything. Then, the rest of my code keeps telling me it can’t open the current file created by the program.

PLEASE HELP! I am losing my mind with this one.

Thank you in advance!

I think I now have the issue sorted out. I think the Arduino had run out of memory? Is there an easy way to check this in the future? The IDE said there was still space…

This is easily possible as the SD library for itself will use almost half of the RAM of an UNO. Your code wastes a lot of memory by not using the F() macro for string literals.

No, the IDE just tells you how much flash the sketch uses and how much RAM the global variables need. It never says that there is still space.

Thanks, Pylon. That makes a lot of sense. I didn’t know about the F() macro…

not sure you’re still interested in available SD memory, but when considering storage devices like an SD, you need to consider the number of blocks that are used. if a file contained just one byte, it still requires a block.

I didn’t know that about the blocks. I am going to need to look into that more in that case. Thank you for the help!

Last step of this project, I need to have a sort of initial file that is stored on the SD card that reads some initial parameters. Basically, when I plug in the SD card, I will have already saved a .txt file to it with a different integer on each line. I would like to read each integer into a different variable. Does anyone have a suggestion where to start? I’m chasing my tail on this one a bit.

need to show the format (binary, ascii) of the data you want to process from the SD?