Go Down

Topic: SdFat deleting oldest file in directory (Read 656 times) previous topic - next topic

carlfarrington

Hi!

I thought I had nailed it!

It looked like the files were always listed in order of creation, therefore a simple file.openNext(&dirFile); file.remove(); would do it, but... it looked great, until I started deleting and creating a few new files.

Now I can see that the new files are created in place of the deleted files :(


I was hoping to not have to parse all the timestamps (I do have proper timestamps, I am using the dateTimecallback with an RTC).

Is there any easy way to delete the oldest file?

This is what I had, but.. it's no good.
Code: [Select]

void deleteOldestFile(){
  SdFile dirFile;
  SdFile file;
  if (!dirFile.open("/", O_READ)) {
    sd.errorHalt("open root failed");
  }
  file.openNext(&dirFile, O_WRITE);
  while (file.isSubDir() | file.isHidden()) {
    file.openNext(&dirFile, O_WRITE);
  }
  file.remove();
  dirFile.close();
}

carlfarrington

OK I have done it this way:

Code: [Select]

void userDeleteOldestFile(){
  cls();
  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();
  }



  Serial.print("Delete file: ");
  oldestFile.printName(&Serial);
  Serial.print(" (Y/N):");
  while (!Serial.available()) {  }
  if (Serial.available()) {
        switch (Serial.read()) {
          case 'y':
          case 'Y': {
            oldestFile.remove();
            dirFile.close();
            Serial.println();
            pause();
            cls();
            break;
          }
          case 'n':
          case 'N': {
            oldestFile.close();
            dirFile.close();
            Serial.println();
            pause();
            cls();
            break;           
          }
         default: {
          oldestFile.close();
          dirFile.close();
          userDeleteOldestFile();
          break;
         }
        }
  }
}


carlfarrington

#2
Feb 19, 2018, 06:34 pm Last Edit: Feb 21, 2018, 10:37 am by carlfarrington
or, the non-interactive version which runs when the free space counter gets below 8mb:
Code: [Select]

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();
  }
  oldestFile.remove();
  dirFile.close();
}

Go Up