我正在使用“LittleFS”
并将 listDir 函数替换为新的“listDel 函数;我遇到的问题是错误的时间戳日期和时间。示例草图的唯一变化是替换函数。
时间戳与此函数正确。
原始库示例函数:
代码:
全选void listDir(const char * dirname) {
Serial.printf(\"Lis
ting 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);
}
}
此函数的时间戳有误。
新的“listDel”功能:
代码:
全选void 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
= strdup(str.c_str());
Serial.print(filelist);
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));
Serial.print(\"Removed: \");
Serial.print(filelist);
Serial.print(\" \");
Serial.print(i);
Serial.println(\"\");
}
i = 0;
}
为什么我使用“listDel”而不是“listDir”时得到错误的时间戳?
附加的修改库示例使用修改后的“ESP8266FtpServer.cpp 与“LittleFS”一起使用。“
LittleFS”是“SPIFFS”的替代品。
“listDel”函数的目的是将文件名放入数组中;然后删除前四个文件。
0