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

Moderator: igrr

User avatar
By Sirquil
#83117 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
Last edited by Sirquil on Mon Jul 15, 2019 4:12 pm, edited 1 time in total.
User avatar
By Sirquil
#83132 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
Last edited by Sirquil on Fri Jul 19, 2019 12:27 am, edited 3 times in total.
User avatar
By Sirquil
#83133 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
Last edited by Sirquil on Thu Jul 18, 2019 10:54 pm, edited 1 time in total.
User avatar
By Sirquil
#83135 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