Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Sirquil
#83873 Here is what I have tried:

Code: Select allvoid fileStore()   
{

     int tempy;
     String Date;
     String Month;

     tempy = (DATE);
     if (tempy < 10)
     {
          Date = ("0" + (String)temp);
     }
     else
     {
          Date = (String)tempy;
     }

     tempy = (MONTH);
     if (temp y< 10)
     {
          Month = ("0" + (String)tempy);
     }
     else
     {
          Month = (String)tempy;
     }

     String logname;  //file format /LOGxxyyzzzz.txt
     logname = "/LOG";
     logname += Date;
     logname += Month;   
     logname += YEAR;
     logname += ".TXT";

     //Open file for appended writing
     File log = SPIFFS.open(logname.c_str(), "a");

     // Open file for appended writing
     //File log = SPIFFS.open("/LOG.TXT", "a");

     if (!log)
     {
          Serial.println("file open failed");
     }

     removeFiles();  //remove oldest four files

}

void removeFiles()
{

     i = 0;
     
     File root = SPIFFS.open("/");

     File file = root.openNextFile();

     root.seek(0, SeekSet);  //same as rewind?

     while(file)
     {

          if(strncmp(file.name(), "/LOG", 4) == 0)
          {

               i++;
               filelist[i] = strdup(file.name());
               Serial.println("");
               Serial.print(filelist[i]);
               Serial.print("  ");
               Serial.print(i);

               

          }

          file = root.openNextFile();
         
     }

     Serial.println("");


     if(i > 4)
     {

          for(i = 1; i < 5; i++) //Delete only first four files; keep from getting too many log files.
          {

               SPIFFS.remove(filelist[i]);
               Serial.print("Removed:  ");
               Serial.print(filelist[i]);
               Serial.print("  ");
               Serial.print(i);
               Serial.println("");

          }

     }

}


fileStore is called at Midnight; creates new LOG file with same naming convention every time pulling date and month GetDateTime function.

Here is a result of the above code:

Code: Select all/LOG20092019.TXT  1
/LOG17092019.TXT  2
/LOG18092019.TXT  3
/LOG19092019.TXT  4
/LOG16092019.TXT  5
Removed:  /LOG20092019.TXT  1
Removed:  /LOG17092019.TXT  2
Removed:  /LOG18092019.TXT  3
Removed:  /LOG19092019.TXT  4


Not the wanted result.

Desired listing of SPIFFS "/LOG*" files:

LOG12092019.TXT
LOG13092019.TXT
LOG14092019.TXT
LOG15092019.TXT
LOG16092019.TXT
LOG17092019.TXT
LOG18092019.TXT
LOG19092019.TXT
LOG20092019.TXT

Is there a way to list SPIFFS, files so the oldest file is the first listed and the newest file is listed last. Goal being to delete the four "oldest" files.

William