You can use the Cardinfo example to see how to determine the total card size. Look in SD/utility/SDvolume::allocContiguous to see how it searches for available clusters to allocate. I don't think it keeps a count of free or allocated clusters so you will have to go through every cluster and count the free ones.

I should have mentioned that I had already successfully tested the card size functionality. I was hoping there was a simple library function call to get the amount of free space on the card...

Another, very kludgy workaround, would be to take the total card size and loop through all of the stored files subtracting their current size from the total size. That would give an indication of how much size is left. That's definitely NOT what I am looking for... especially, as I think of it, as it would have to recurse through folders and subfolders as well!

I am on day 4 or 5 with my first Arduino so still finding out all about the various programming structures...

I naïvely thought I might be able to at least use the SdFile.ls() function, as it recurses through all files and folders on the card, and deduct each file size from the total size to give a file-based free size amount. Then I realised I have no idea where any of these undocumented classes from the SD.H library (Sd2Card, SdVolume and SdFile) are...

I assume this, relatively basic, free / available space functionality will be added to an upgraded version of the sd.h library?

From what I understand, which is obviously less than I think, all of the classes I mentioned (Sd2Card, SdVolume and SdFile) must be part of the SD.H library. They are used in the classic tutorial example of getting card info:

I have installed the SdFat library and have been having fun seeing what I can do with it.

I got a FreeSpace function up and running very quickly. I then found a TimeStamp function in the library examples and that was something I was really looking for so I played around with that, for way too long, and can now set file creation, modification and accessed datetime stamps.

I went back to my file I/O operations and played with opening, writing to and closing my files with the new library. That works fine.

However all of this leads me to 2 questions:

How can I read/display the size of my file?With the SD library I would probably use the ls object with a LS_SIZE call. But how do I do this for my individual file object?

How do I recalculate the FreeSpace as I get the feeling it's showing me cached info or something.

In my loop() I read the free space, write a line to my file wait 10 seconds and do it all again. The amount of free space never changes although I can see, afterwards, that my lines are being properly appended to my file and its size is getting bigger and bigger.

You are not going to see free space on the SD change unless a new cluster is allocated to a file. That's why Sdfat has the function freeClusterCount(), which returns how many allocation units are available on the SD.

Notice that the free cluster count decreased by one after the first write and then didn't change. This is because one cluster of 32768 bytes was allocated to the file on the first write. The cluster count won't decrease until the file is larger than 32768 bytes an then another cluster will be allocated..

Wow! Thank you for your prompt and oh so helpful reply. The most important info I needed was the fact that I wasn't going to see a change in the free space until another cluster was allocated to the file. My code was therefore actually already working from that point of view...

The simple fileSize() call was also the simple answer to what I needed. I had tried without the uppercase S...

I have removed the timestamp stuff from my code, for brevity here, it does just what I want it to...

[I got bored waiting for that new cluster to appear so I made it write 100 lines at a time and reduced the delay to 10 seconds)