How to delete file from SD card with SdFat

1

I'm trying to delete the oldest file from an sd card with SdFat with no success

I've found that function on Arduino forums Here

void deleteOldestFile(){
  SdFile dirFile;
  SdFile file;
  SdFile oldestFile;

  dir_t dir;
  uint32_t oldestModified = 0xFFFFFFFF;
  uint32_t lastModified;

  if (!dirFile.open("/", O_READ)) {
    sd.errorHalt("open root failed");
  }

  while (file.openNext(&dirFile, O_WRITE)) {
    // Skip directories and hidden files.
    if (!file.isSubDir() && !file.isHidden()) {
      file.dirEntry(&dir);
      lastModified = (uint16_t (dir.lastWriteDate) << 16 | dir.lastWriteTime);
      if (lastModified < oldestModified ) {
        oldestModified = lastModified;
        oldestFile = file;
      }
    }
    file.close();
  }
  if(!oldestFile.remove()) Serial.println("failed");
  dirFile.close();
}

but I'm getting a "failed" message when this function run any idea why and how to fix this problem?

EDIT: I can use remove like that:

  char delChar[13] = {0};
...
  Serial.print("oldest: ");
  oldestFile.printName();
  oldestFile.getName(delChar,13);

  Serial.println();
  Serial.print("delchar: ");
  Serial.println(delChar);
  if(!sd.remove(delChar)) Serial.println("fail");
  dirFile.close();

but the file.remove() still not works

arduino
sd-card
arduino-c++
asked on Stack Overflow Sep 23, 2019 by Daniel Surizon • edited Sep 24, 2019 by Daniel Surizon

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0