-->
Page 1 of 2

SPIFFS Remove first four files of eight in array

PostPosted: Sun Jul 14, 2019 2:49 pm
by Sirquil
Trying to get list of filenames with index number of filename. Will remove filename based on index number to retrieve filename from array. First four filenames will be removed -- out of eight files listed with first, four characters "/LOG."

Plan to use in a function to be called when index number reaches eight, to remove files.

Code: Select all#include "SPIFFS.h"

char* filelist[12][8];
char i;
char j;
 
void setup() {
 
  Serial.begin(115200);

  if (!SPIFFS.begin(true)) { //Dev board is a ESP32
  Serial.println("An Error has occurred while mounting SPIFFS");
  return;
  }

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

  File file = root.openNextFile();

  while(file)
  {

   if(strncmp(file.name(), "/LOG", 4) == 0)
   {     
   
    j++;
    strcpy(filelist[i][j], file.name());
    Serial.print(filelist[i][j]);
    Serial.print("  " + j);
    }
   
   

    file = root.openNextFile();
   
  }
 
  for(j = 0;j < 4; j++)  //Delete only first four files; keep from getting too many log files.
  {
    SPIFFS.remove(filelist[i][j]);
    Serial.print("Removed:  ");
    Serial.println(filelist[i][j]);
  }
 
}
void loop() {}
 


First time working with arrays. Having problem getting filename into array and adding index number.

Code compiles. Requesting help solving core panic.

William

Four files hold about one month of data; plan to retain a month of data always on web server.

William

SPIFFS Remove first four files of eight in array

PostPosted: Mon Jul 15, 2019 4:11 pm
by Sirquil
Working now...

global: char* filelist[12];

Code: Select allvoid removeFiles()
{

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

  File file = root.openNextFile();

  i = 0;
 
  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 == 8)
      {
         
          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("");
     
          }
         
      }
     
 


William

SPIFFS Remove first four files of eight in arra

PostPosted: Mon Jul 15, 2019 6:39 pm
by Sirquil
I use the following code for naming log files:

Code: Select allvoid fileStore()   
{


     String logname;
     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");
     }
}


How can a leading zero be added to DATE and MONTH in this "fileStore function?

William

SPIFFS Remove first four files of eight in arra

PostPosted: Tue Jul 16, 2019 7:05 am
by Sirquil
Wanted the latest filename (by date) to be the last file in the listfile array.

Ended up with this code:
Code: Select allvoid fileStore() 
{

     count++;
 
    int temp;
    String Date;
    String Month;

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

     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");

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

     if(countFiles = 8)
     {
          removeFiles();

          countFiles = 0;
     }

}


William