Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved How to skip over or filter out "." and ".." from QDir?

    General and Desktop
    5
    7
    374
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Guerrian
      Guerrian last edited by Guerrian

      I am using QDir to read all files in a directory:

      QDir directory(dn);
      QStringList files = directory.entryList();
      foreach(QString fn, files)
          processFile(dn + '/' + fn);
      

      However I get the files "." and "..". How do I skip over these or filter them out?

      This seems like a messy way of doing it:

      if (fn != ".." && fn != ".")
          processFile(dn + '/' + fn);
      

      Linux Mint 18.3
      Qt 5.14.1
      Qt Creator 4.11.1

      1 Reply Last reply Reply Quote 0
      • K
        KaoN last edited by

        QDir dir;
        dir.setFilter(QDir::NoDotAndDotDot);
        
        Guerrian 1 Reply Last reply Reply Quote 5
        • Kent-Dorfman
          Kent-Dorfman last edited by

          If you go to google and type in QDir, the very first result points you to this really cool thing called on-line class documentation. ... Amazing! Give it a try.

          1 Reply Last reply Reply Quote 2
          • D
            damianatorrpm last edited by

            @Guerrian said in How to skip over or filter out "." and ".." from QDir?:

            if (fn != ".." && fn != ".")

            It's the best solution. For me NoDotAndDotDot never works correctly (e.g directories having dot in name)

            jsulm Guerrian 2 Replies Last reply Reply Quote 0
            • jsulm
              jsulm Lifetime Qt Champion @damianatorrpm last edited by

              @damianatorrpm said in How to skip over or filter out "." and ".." from QDir?:

              For me NoDotAndDotDot never works correctly (e.g directories having dot in name)

              Because it only refers to . and .. not to files starting with .

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply Reply Quote 3
              • Guerrian
                Guerrian @damianatorrpm last edited by Guerrian

                @damianatorrpm This worked for me:

                    QDir directory(dn);
                    QStringList files = directory.entryList(QDir::NoDotAndDotDot | QDir::AllEntries);
                    foreach(QString fn, files)
                        slvFl(dn + '/' + fn);
                

                Linux Mint 18.3
                Qt 5.14.1
                Qt Creator 4.11.1

                1 Reply Last reply Reply Quote 1
                • Guerrian
                  Guerrian @KaoN last edited by

                  @KaoN I think it needs to be:

                  QDir::NoDotAndDotDot | QDir::AllEntries
                  

                  Linux Mint 18.3
                  Qt 5.14.1
                  Qt Creator 4.11.1

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post