Is there a documented, standard library for manipulating files on an SD card?

Hi, I'm aware of the SD library and I use it. However, it seems like it isn't really meant to be used for things like listing the files on the SD card. For example, the example program CardInfo has:

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

And those aren't SD or File objects, but, for example, the SdFile object is used to list the files on the card. The SD library documentation doesn't mention anything about those.

Is there a place they're documented? Or, is there a better library to use?

It seems like this guy about 4 years ago came upon the same problem, and was advised to use something called Sdfat, but I checked in the library repositories in the IDE and didn't find anything like that.

Does this help ? SDFAT documentation - Storage - Arduino Forum

The SD library is a wrapper around some basic classes in the SD\utility directory, written by William Greiman. That SdFat library and documentation can be found on GitHub.

ranchmebro:
Hi, I'm aware of the SD library and I use it. However, it seems like it isn't really meant to be used for things like listing the files on the SD card.

If it wasn't meant to be used for things like listing the files on the SD card, why did they supply the 'listfiles' example showing exactly how to do that?!?

It's a weak excuse to present selective examples, without or instead of any reference to the used library and documentation :frowning:

I only agree that using the lower level classes requires a programming background that also should be sufficient to find out more about the sparsely documented library functions, FAT structures and other file system related items. Most of these topics are beyond a specific library documentation, and entire books have already been written about them.

DrDiettrich:
The SD library is a wrapper around some basic classes in the SD\utility directory, written by William Greiman. That SdFat library and documentation can be found on GitHub.

Thanks a bunch! I'll check it out.

I want, for example, a way to delete all the files on my SD card. SD comes with a remove() function, but unfortunately not an easy way to actually get the names of all the files...

johnwasser:
If it wasn't meant to be used for things like listing the files on the SD card, why did they supply the 'listfiles' example showing exactly how to do that?!?

That's exactly what I, and the guy in the thread I linked, were wondering :slight_smile:

6v6gt:
Does this help ? SDFAT documentation - Storage - Arduino Forum

That thread was in my OP :slight_smile:

ranchmebro:
I want, for example, a way to delete all the files on my SD card. SD comes with a remove() function, but unfortunately not an easy way to actually get the names of all the files...

I think this should do what you want, using the documented functions of the SD library:

#include <SPI.h>
#include <SD.h>
File root;
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  root = SD.open("/");
  removeContents(root, "/");
  
  root.close();
  Serial.println("done!");
}
void loop() {
  // nothing happens after setup finishes.
}
void removeContents(File dir, String dir_path) {
  while (true) {
    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      break;
    }
    String file_path = dir_path + "/" + entry.name();
    if (entry.isDirectory()) {
      removeContents(entry, file_path);
      entry.close();
      SD.rmdir(file_path);
      dir.rewindDirectory();
    } else {
      entry.close();
      SD.remove(file_path);
      dir.rewindDirectory();
    }
  }
}

Excellent, thank you!