0

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

asked Jun 8, 2020 at 14:14
6
  • And you're sure that the filenames do begin with precisely the characters /LOG ? Commented Jun 8, 2020 at 14:24
  • Just a sanity check here. But isn't / 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. Commented Jun 8, 2020 at 14:29
  • @Kwasmich Unless the result of dir.fileName() is the fully qualified absolute path of the file. Which is why I was asking if the filenames really do start /LOG Commented Jun 8, 2020 at 14:32
  • @Majenko true. Just to make sure nothing gets overlooked. strncmp is case-sensitive! Commented Jun 8, 2020 at 15:24
  • @Kwasmich Indeed. Not a problem if it's using 8.3 filenames, but could be a problem if the FS uses LFN. Commented Jun 8, 2020 at 15:35

1 Answer 1

2

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

answered Jun 8, 2020 at 15:08
5
  • FileZilla shows filename as "LOG0608.TXT." Naming convention is "LOG + Month + Date + '.TXT'." I could have carried the "/" over from the web server. Commented 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. Commented Jun 8, 2020 at 17:54
  • Dose dir.fileName() gives a string as "LOGXXXX.TXT" or "/LOGXXXX.TXT" Commented Jun 8, 2020 at 17:59
  • 1
    Used: String file_name = dir.fileName(); if(file_name.startsWith("LOG")) { Thanks! Commented Jun 8, 2020 at 18:06
  • gr8 glad I can help you Commented Jun 8, 2020 at 18:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.