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

  • And you're sure that the filenames do begin with precisely the characters /LOG ? – Majenko Jun 8 '20 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. – Kwasmich Jun 8 '20 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 – Majenko Jun 8 '20 at 14:32
  • @Majenko true. Just to make sure nothing gets overlooked. strncmp is case-sensitive! – Kwasmich Jun 8 '20 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. – Majenko Jun 8 '20 at 15:35
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

  • FileZilla shows filename as "LOG0608.TXT." Naming convention is "LOG + Month + Date + '.TXT'." I could have carried the "/" over from the web server. – William Jun 8 '20 at 17:45
  • Need "dir.fileName()" to compare acutal filename to LOG0608.TXT. I only want files starting with "LOG" to be displayed. – William Jun 8 '20 at 17:54
  • Dose dir.fileName() gives a string as "LOGXXXX.TXT" or "/LOGXXXX.TXT" – Avon97 Jun 8 '20 at 17:59
  • 1
    Used: String file_name = dir.fileName(); if(file_name.startsWith("LOG")) { Thanks! – William Jun 8 '20 at 18:06
  • gr8 glad I can help you – Avon97 Jun 8 '20 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.