Reading from SPIFFS

For the following code
it should show recent file in SPIFFS but it do not work
Serial monitor output

Current time From Internal RTC: 2023-5-25 17:31:39
----DIR: /ROOT:
FILE: /012023.txt
FILE: /022023.txt
FILE: /032023.txt
FILE: /042023.txt
FILE: /082022.txt
FILE: /092022.txt
FILE: /052023.txt
No recent files found.
Recent Month: 5
Recent Year: 2023

it should say Recent file 052023.txt

what is wrong with my code ??
please help

#include <WiFi.h>
#include <WebServer.h>
#include "time.h"
#include <string>
#include <iostream>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <time.h>
#include <SPIFFS.h>

#define STASSID "AMGAD60"
#define STAPSK  "Amgad#60"
const char* ssid = STASSID;
const char* password = STAPSK;

const char* ntpServer = "pool.ntp.org";
const long gmtOffsetInSeconds = 10800;
const int daylightOffsetInSeconds = 10800;

bool wifiConnected = false;
void connectToWiFi() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print  ("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("Connected to WiFi");
  wifiConnected = true;
}



void listDir(char* dir) {
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time from internal RTC");
    return;
  }
int recentMonth = timeinfo.tm_mon + 1;  // Month is 0-based, so adding 1 to get the actual month
int recentYear = timeinfo.tm_year + 1900;  // Year is years since 1900, so adding 1900 to get the actual year

  File root = SPIFFS.open(dir);
  File recentFile;
  String recentFileName = "";

  File file = root.openNextFile();

  while (file) {
    String fileName = file.name();
    if (fileName.endsWith(".txt") && fileName.length() == 12) {
      if (recentFileName.isEmpty()) {
        recentFile = file;
        recentFileName = fileName;
        recentMonth = recentFileName.substring(0, 2).toInt();
        recentYear = recentFileName.substring(2, 6).toInt();
      } else {
        int fileMonth = fileName.substring(0, 2).toInt();
        int fileYear = fileName.substring(2, 6).toInt();

        if (fileYear > recentYear || (fileYear == recentYear && fileMonth > recentMonth)) {
          recentFile = file;
          recentFileName = fileName;
          recentMonth = fileMonth;
          recentYear = fileYear;
        }
      }
    }

    Serial.print("FILE: ");
    Serial.println(fileName);

    file = root.openNextFile();
  }
  root.close();

  if (!recentFileName.isEmpty()) {
    Serial.print("Recent File: ");
    Serial.println(recentFileName);
    Serial.print("Recent Month: ");
    Serial.println(recentMonth);
    Serial.print("Recent Year: ");
    Serial.println(recentYear);
  } else {
    Serial.println("No recent files found.");
    Serial.print("Recent Month: ");
    Serial.println(recentMonth);
    Serial.print("Recent Year: ");
    Serial.println(recentYear);
  }
}  






void setup() {
  Serial.begin(115200);
connectToWiFi();
  
  WiFiUDP ntpUDP;
  NTPClient timeClient(ntpUDP, ntpServer, gmtOffsetInSeconds, daylightOffsetInSeconds);

  // Set ESP32 internal RTC from NTP
  timeClient.begin();
  timeClient.update();

  // Wait for NTP response
  while (!timeClient.update()) {
    Serial.println("Waiting for NTP response...");
    delay(1000);
  }

  // Get the epoch time from NTP
  unsigned long epochTime = timeClient.getEpochTime();

  // Set the ESP32's internal RTC
  struct timeval tv;
  tv.tv_sec = epochTime;
  tv.tv_usec = 0;
  settimeofday(&tv, NULL);

struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time from internal RTC");
    return;
  }
int currentMonth = timeinfo.tm_mon + 1;  // Month is 0-based, so adding 1 to get the actual month
int currentYear = timeinfo.tm_year + 1900;  // Year is years since 1900, so adding 1900 to get the actual year

Serial.print("Current time From Internal RTC: ");
  Serial.print(timeinfo.tm_year + 1900);
  Serial.print("-");
  Serial.print(timeinfo.tm_mon + 1);
  Serial.print("-");
  Serial.print(timeinfo.tm_mday);
  Serial.print(" ");
  Serial.print(timeinfo.tm_hour);
  Serial.print(":");
  Serial.print(timeinfo.tm_min);
  Serial.print(":");
  Serial.print(timeinfo.tm_sec);
  Serial.println();
 
  if (!SPIFFS.begin(true)) {
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }

  Serial.println("\n----DIR: /ROOT:");
  listDir("/");
}

void loop() {}

Because your condition isn't fulfilled for any of the files. They have length 11.

BTW, these string splitting won't work too:

At index 0 is the slash ('/').

You are right thanks for your help

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.