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 get only folder count in a directory
Forum Updated to NodeBB v4.3 + New Features

how to get only folder count in a directory

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 6.4k Views 1 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #3

    Hi
    there is also

    QDirIterator it("/etc", QDirIterator::Subdirectories);
    while (it.hasNext()) {
        qDebug() << it.next();
    

    http://doc.qt.io/qt-5/qdiriterator.html#details

    A 1 Reply Last reply
    2
    • mrjjM mrjj

      Hi
      there is also

      QDirIterator it("/etc", QDirIterator::Subdirectories);
      while (it.hasNext()) {
          qDebug() << it.next();
      

      http://doc.qt.io/qt-5/qdiriterator.html#details

      A Offline
      A Offline
      ARASHz4
      wrote on last edited by
      #4

      @mrjj said in how to get only folder count in a directory:

      QDirIterator::Subdirectories

      i want get only folder count in current directory not sub directories

      JonBJ mrjjM 3 Replies Last reply
      0
      • A ARASHz4

        @mrjj said in how to get only folder count in a directory:

        QDirIterator::Subdirectories

        i want get only folder count in current directory not sub directories

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #5

        @ARASHz4
        That's what my entryList above code approach does (pretty sure). Why don't you try it and see that's what it returns, and does not recurse downward?

        1 Reply Last reply
        2
        • A ARASHz4

          @mrjj said in how to get only folder count in a directory:

          QDirIterator::Subdirectories

          i want get only folder count in current directory not sub directories

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #6

          @ARASHz4
          Ok, so only first level of subdirectories.
          Try with QDir::entryList() then as @JNBarchan suggests.

          1 Reply Last reply
          1
          • A ARASHz4

            @mrjj said in how to get only folder count in a directory:

            QDirIterator::Subdirectories

            i want get only folder count in current directory not sub directories

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #7

            @ARASHz4
            Or, if you do want to use QDirIterator, you need to use the QDirIterator::QDirIterator(const QString &path, QDir::Filters filters, IteratorFlags flags = NoIteratorFlags) overload and set filters (not flags) as per what I wrote earlier.

            A 1 Reply Last reply
            0
            • JonBJ JonB

              @ARASHz4
              Or, if you do want to use QDirIterator, you need to use the QDirIterator::QDirIterator(const QString &path, QDir::Filters filters, IteratorFlags flags = NoIteratorFlags) overload and set filters (not flags) as per what I wrote earlier.

              A Offline
              A Offline
              ARASHz4
              wrote on last edited by
              #8

              @JNBarchan i try :

              QDir(FolderAddress).entryList(QDir::AllDirs | QDir::NoDotAndDotDot).count()
              

              this return folder count in a directory

              thank you

              JonBJ 1 Reply Last reply
              0
              • A ARASHz4

                @JNBarchan i try :

                QDir(FolderAddress).entryList(QDir::AllDirs | QDir::NoDotAndDotDot).count()
                

                this return folder count in a directory

                thank you

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #9

                @ARASHz4
                That's right, and just what I suggested :(

                Note that this simplest code builds a list of all directories in memory and then counts them. I think that's fine for a reasonable number of sub-directories. However, if you have like 1,000+ that's where QDirIterator approach would be more efficient (memory-wise) by not building a list, but requires a touch more lines of code, if you ever feel like it.

                T A 2 Replies Last reply
                2
                • JonBJ JonB

                  @ARASHz4
                  That's right, and just what I suggested :(

                  Note that this simplest code builds a list of all directories in memory and then counts them. I think that's fine for a reasonable number of sub-directories. However, if you have like 1,000+ that's where QDirIterator approach would be more efficient (memory-wise) by not building a list, but requires a touch more lines of code, if you ever feel like it.

                  T Offline
                  T Offline
                  t3685
                  wrote on last edited by
                  #10

                  @JNBarchan

                  You can call http://doc.qt.io/qt-5/qdiriterator.html#fileInfo and http://doc.qt.io/qt-5/qfileinfo.html#isDir to only count directories.

                  JonBJ 1 Reply Last reply
                  1
                  • T t3685

                    @JNBarchan

                    You can call http://doc.qt.io/qt-5/qdiriterator.html#fileInfo and http://doc.qt.io/qt-5/qfileinfo.html#isDir to only count directories.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #11

                    @t3685
                    But that is likely to be "inefficient", since you have to visit every entry (files as well as directories) in your code and then test whether it's a directory. That's the whole point of using the QDir::Filters approach, which (presumably) allows the Qt code to use whatever native function might be available to only pick out directories in the first place....

                    A 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @ARASHz4
                      That's right, and just what I suggested :(

                      Note that this simplest code builds a list of all directories in memory and then counts them. I think that's fine for a reasonable number of sub-directories. However, if you have like 1,000+ that's where QDirIterator approach would be more efficient (memory-wise) by not building a list, but requires a touch more lines of code, if you ever feel like it.

                      A Offline
                      A Offline
                      ARASHz4
                      wrote on last edited by
                      #12

                      @JNBarchan can you show me a example with QDirIterator for get folder count

                      JonBJ 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @t3685
                        But that is likely to be "inefficient", since you have to visit every entry (files as well as directories) in your code and then test whether it's a directory. That's the whole point of using the QDir::Filters approach, which (presumably) allows the Qt code to use whatever native function might be available to only pick out directories in the first place....

                        A Offline
                        A Offline
                        ARASHz4
                        wrote on last edited by
                        #13

                        @JNBarchan I think this code no builds a list of all directories in memory

                        QDir folder (FolderAddress);
                        folder.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);
                        int folderCount = folder.count();
                        
                        JonBJ 1 Reply Last reply
                        0
                        • A ARASHz4

                          @JNBarchan can you show me a example with QDirIterator for get folder count

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #14

                          @ARASHz4
                          From @mrjj 's earlier suggestion, just modify to:

                          QDirIterator it("/etc", QDir::AllDirs | QDir::NoDotAndDotDot);
                          while (it.hasNext()) {
                              qDebug() << it.next();
                          

                          and count them. This way does not build a list in memory.

                          1 Reply Last reply
                          3
                          • A ARASHz4

                            @JNBarchan I think this code no builds a list of all directories in memory

                            QDir folder (FolderAddress);
                            folder.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);
                            int folderCount = folder.count();
                            
                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #15

                            @ARASHz4 said in how to get only folder count in a directory:

                            @JNBarchan I think this code no builds a list of all directories in memory

                            QDir folder (FolderAddress);
                            folder.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);
                            int folderCount = folder.count();
                            

                            I believe that way does build a complete list, internally, and then evaluates the count() on the list. Need to use QDirIterator instead of QDir if want be sure no list built.

                            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