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.
  • A Offline
    A Offline
    ARASHz4
    wrote on 16 Nov 2017, 10:14 last edited by
    #1

    hi
    i want get only count of folders in a directory not files

    J 1 Reply Last reply 16 Nov 2017, 10:19
    0
    • A ARASHz4
      16 Nov 2017, 10:14

      hi
      i want get only count of folders in a directory not files

      J Offline
      J Offline
      JonB
      wrote on 16 Nov 2017, 10:19 last edited by JonB
      #2

      @ARASHz4
      QDir::entryList(), QDir::Filter, QDir::AllDirs (maybe QDir::NoDotAndDotDot, and not QDir::Files).

      1 Reply Last reply
      3
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 16 Nov 2017, 10:21 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 16 Nov 2017, 10:28
        2
        • M mrjj
          16 Nov 2017, 10:21

          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 16 Nov 2017, 10:28 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

          J M 3 Replies Last reply 16 Nov 2017, 10:38
          0
          • A ARASHz4
            16 Nov 2017, 10:28

            @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

            J Offline
            J Offline
            JonB
            wrote on 16 Nov 2017, 10:38 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
              16 Nov 2017, 10:28

              @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

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 16 Nov 2017, 10:40 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
                16 Nov 2017, 10:28

                @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

                J Offline
                J Offline
                JonB
                wrote on 16 Nov 2017, 10:45 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 16 Nov 2017, 10:58
                0
                • J JonB
                  16 Nov 2017, 10:45

                  @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 16 Nov 2017, 10:58 last edited by
                  #8

                  @JNBarchan i try :

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

                  this return folder count in a directory

                  thank you

                  J 1 Reply Last reply 16 Nov 2017, 11:02
                  0
                  • A ARASHz4
                    16 Nov 2017, 10:58

                    @JNBarchan i try :

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

                    this return folder count in a directory

                    thank you

                    J Offline
                    J Offline
                    JonB
                    wrote on 16 Nov 2017, 11:02 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 16 Nov 2017, 11:14
                    2
                    • J JonB
                      16 Nov 2017, 11:02

                      @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 16 Nov 2017, 11:14 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.

                      J 1 Reply Last reply 16 Nov 2017, 11:19
                      1
                      • T t3685
                        16 Nov 2017, 11:14

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

                        J Offline
                        J Offline
                        JonB
                        wrote on 16 Nov 2017, 11:19 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 16 Nov 2017, 11:35
                        0
                        • J JonB
                          16 Nov 2017, 11:02

                          @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 16 Nov 2017, 11:22 last edited by
                          #12

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

                          J 1 Reply Last reply 16 Nov 2017, 11:37
                          0
                          • J JonB
                            16 Nov 2017, 11:19

                            @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 16 Nov 2017, 11:35 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();
                            
                            J 1 Reply Last reply 16 Nov 2017, 11:40
                            0
                            • A ARASHz4
                              16 Nov 2017, 11:22

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

                              J Offline
                              J Offline
                              JonB
                              wrote on 16 Nov 2017, 11:37 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
                                16 Nov 2017, 11:35

                                @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();
                                
                                J Offline
                                J Offline
                                JonB
                                wrote on 16 Nov 2017, 11:40 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

                                1/15

                                16 Nov 2017, 10:14

                                • Login

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