Chat freely about anything...

User avatar
By Sirquil
#87233 I am using the "LittleFS"library example and replacing thelistDir function with new "listDel function; issue I have is wrong timestamp date and time. Only change from example sketch was replacing the function.

Timestamps are correct with this function.
Original library example function:

Code: Select allvoid listDir(const char * dirname) {
  Serial.printf("Listing directory: %s\n", dirname);

  Dir root = LittleFS.openDir(dirname);

  while (root.next()) {
    File file = root.openFile("r");
    Serial.print("  FILE: ");
    Serial.print(root.fileName());
    Serial.print("  SIZE: ");
    Serial.print(file.size());
    time_t cr = file.getCreationTime();
    time_t lw = file.getLastWrite();
    file.close();
    struct tm * tmstruct = localtime(&cr);
    Serial.printf("    CREATION: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
    tmstruct = localtime(&lw);
    Serial.printf("  LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
  }
}


Timestamps are wrong with this function.
New "listDel" function:

Code: Select allvoid listDel(const char * dirname) {
  Serial.printf("Listing directory: %s\n", dirname);

  Dir root = LittleFS.openDir("/");

  while(root.next())
  {

    File file = root.openFile("r");
 
    String str = root.fileName();
         
    i++;
    filelist[i] = strdup(str.c_str());
    Serial.print(filelist[i]);
    Serial.print("  ");
    Serial.print(i);
    Serial.println("");
    file = file.openNextFile();
    time_t cr = file.getCreationTime();
    time_t lw = file.getLastWrite();
    file.close();
    struct tm * tmstruct = localtime(&cr);
    Serial.printf("    CREATION: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
    tmstruct = localtime(&lw);
    Serial.printf("  LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);

   
  }
   
  for(i = 1;i < 5; i++)  //Delete only first four files; keep from getting too many log files.
  {
     
      LittleFS.remove("/" + String(filelist[i]));
      Serial.print("Removed:  ");
      Serial.print(filelist[i]);
      Serial.print("  ");
      Serial.print(i);
      Serial.println("");

  }
  i = 0; 
   

   
}



Why do I have wrong Timestamp using "listDel" instead of "listDir?"

Attached modified library example uses a modified "ESP8266FtpServer.cpp for use with "LittleFS."
"LittleFS" is the replacement for "SPIFFS."

Purpose of "listDel" function is put filenames into an array; then to delete the first four files.

William
Attachments
Replaced "listDir" function with "listDel" function; produces wronf timestamp date and time.
(2.09 KiB) Downloaded 250 times
User avatar
By Sirquil
#87238 Serial Monitor output for "listDir" function and "listDel" function are attached.

William
Attachments
Wrong timestamp --LittleFS --listDsl function
(2.4 KiB) Downloaded 217 times
Correct timestamp --LittleFS --listDir function
(4.57 KiB) Downloaded 214 times
User avatar
By Sirquil
#87242 Found line of code that was interferring with getting a valid timestamp.


Code: Select allvoid listDel(const char * dirname) {
  Serial.printf("Function listDel --Listing directory: %s\n", dirname);

  Dir root = LittleFS.openDir("/");

  while(root.next())
  {

    File file = root.openFile("r");
 
    String str = root.fileName();
         
    i++;
    filelist[i] = strdup(str.c_str());
    Serial.print(filelist[i]);
    Serial.print("  ");
    Serial.print(i);
    Serial.println("");
    //file = file.openNextFile();    --Not required; was interferring with timestamp
    time_t cr = file.getCreationTime();
    time_t lw = file.getLastWrite();
    file.close();
    struct tm * tmstruct = localtime(&cr);
    Serial.printf("    CREATION: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
    tmstruct = localtime(&lw);
    Serial.printf("  LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);

   
  }
   
  for(i = 1;i < 5; i++)  //Delete only first four files; keep from getting too many log files.
  {
     
      LittleFS.remove("/" + String(filelist[i]));
      Serial.print("Removed:  ");
      Serial.print(filelist[i]);
      Serial.print("  ");
      Serial.print(i);
      Serial.println("");

  }
  i = 0; 
   

   
}


William
Attachments
Correct timestamp produced by "listDel" function now.
(9.1 KiB) Downloaded 230 times