Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to skip over or filter out "." and ".." from QDir?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 1.2k Views 2 Watching
  • 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.
  • GuerrianG Offline
    GuerrianG Offline
    Guerrian
    wrote on last edited by Guerrian
    #1

    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
    0
    • K Offline
      K Offline
      KaoN
      wrote on last edited by
      #2
      QDir dir;
      dir.setFilter(QDir::NoDotAndDotDot);
      
      GuerrianG 1 Reply Last reply
      5
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #3

        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
        2
        • D Offline
          D Offline
          damianatorrpm
          wrote on last edited by
          #4

          @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)

          jsulmJ GuerrianG 2 Replies Last reply
          0
          • D damianatorrpm

            @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)

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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
            3
            • D damianatorrpm

              @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)

              GuerrianG Offline
              GuerrianG Offline
              Guerrian
              wrote on last edited by Guerrian
              #6

              @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
              1
              • K KaoN
                QDir dir;
                dir.setFilter(QDir::NoDotAndDotDot);
                
                GuerrianG Offline
                GuerrianG Offline
                Guerrian
                wrote on last edited by
                #7

                @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
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved