Trying to list root directory of LittleFS; but only files beginging with "/LOG."
String str;
if (!LittleFS.begin())
{
Serial.println("LittleFS failed to mount !");
}
Dir dir = LittleFS.openDir("/");
while (dir.next())
{
if(strncmp(dir.fileName().c_str(), "/LOG", 4) == 0)
{
str += "<a href=\"";
str += dir.fileName();
str += "\">";
str += dir.fileName();
str += "</a>";
str += " ";
str += dir.fileSize();
str += "<br>\r\n";
}
}
client.print(str);
If I comment out "if(strncmp(dir.fileName().c_str(), "/LOG", 4) == 0)" and associated braces; all files list. Code with "if(strncmp(dir.fileName().c_str(), "/LOG", 4) == 0)" no files are listed.
How can I list only files that begin with "/LOG?"
William
1 Answer 1
Give a filename String to text...
String text = "/LOGsdfsdf";
if(text.startsWith("/LOG")){
//wee file begins with /LOG
}else{
// wops wrong file.
}
Look in here
https://www.arduino.cc/reference/en/language/variables/data-types/stringobject/
Method url startsWith("");
https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/startswith
-
FileZilla shows filename as "LOG0608.TXT." Naming convention is "LOG + Month + Date + '.TXT'." I could have carried the "/" over from the web server.William– William2020年06月08日 17:45:44 +00:00Commented Jun 8, 2020 at 17:45
-
Need "dir.fileName()" to compare acutal filename to LOG0608.TXT. I only want files starting with "LOG" to be displayed.William– William2020年06月08日 17:54:23 +00:00Commented Jun 8, 2020 at 17:54
-
Dose dir.fileName() gives a string as "LOGXXXX.TXT" or "/LOGXXXX.TXT"Avon97– Avon972020年06月08日 17:59:11 +00:00Commented Jun 8, 2020 at 17:59
-
1Used: String file_name = dir.fileName(); if(file_name.startsWith("LOG")) { Thanks!William– William2020年06月08日 18:06:42 +00:00Commented Jun 8, 2020 at 18:06
-
/LOG
?/
used as path separator? In my world/
is an invalid character for file or directory names. Listed file names in directory/
should not contain/
then./LOG
strncmp
is case-sensitive!