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. Count File in a folder (include subfolder)
Forum Update on Monday, May 27th 2025

Count File in a folder (include subfolder)

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 607 Views
  • 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.
  • J Offline
    J Offline
    jackventor
    wrote on 30 Sept 2020, 18:43 last edited by
    #1

    hi i want to know how much file are in a folder (include subfolder) i find some code in internet but all of them only count file in a folder and not its subfolder
    like this codes

    #include <stdio.h>
    #include <dirent.h>
    
    int main(int argc, char *argv[])
    {
        if(argc != 2)
        {
            printf("Usage: ./count \"<path>\"\n");
            return 1;
        }
    
        struct dirent *de;
        DIR *dir = opendir(argv[1]);
        if(!dir)
        {
            printf("opendir() failed! Does it exist?\n");
            return 1;
        }
    
        unsigned long count=0;
            while(de = readdir(dir))
         {
              ++count;
         }
    
        closedir(dir);
        printf("%lu\n", count);
    
        return 0;
    }
    

    what is best and faster way to count all file in a folder and its subfolders?
    thanks

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 30 Sept 2020, 18:45 last edited by
      #2

      Hi and welcome to devnet,

      Since you are using Qt, you should check QDirIterator.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply 30 Sept 2020, 19:03
      2
      • S SGaist
        30 Sept 2020, 18:45

        Hi and welcome to devnet,

        Since you are using Qt, you should check QDirIterator.

        J Offline
        J Offline
        jackventor
        wrote on 30 Sept 2020, 19:03 last edited by jackventor
        #3

        @SGaist is this code right?
        it working

        QDirIterator it("/sys/", QDir::Files, QDirIterator::Subdirectories);
            while (it.hasNext()) {
                if(it.next() > 0 )
                {
                    howfile++;
                }
            }
            qDebug() << howfile;
        

        i first try to just delete it.next() but its make while infinite

        K 1 Reply Last reply 1 Oct 2020, 07:05
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 30 Sept 2020, 19:32 last edited by
          #4

          Looks correct yes

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • J jackventor
            30 Sept 2020, 19:03

            @SGaist is this code right?
            it working

            QDirIterator it("/sys/", QDir::Files, QDirIterator::Subdirectories);
                while (it.hasNext()) {
                    if(it.next() > 0 )
                    {
                        howfile++;
                    }
                }
                qDebug() << howfile;
            

            i first try to just delete it.next() but its make while infinite

            K Offline
            K Offline
            KroMignon
            wrote on 1 Oct 2020, 07:05 last edited by KroMignon 10 Jan 2020, 07:06
            #5

            @jackventor said in Count File in a folder (include subfolder):

            is this code right?

            I don't think so.
            QDirIterator::next() is returning a QString containing file name, it is not a boolean!

            This looks better to me:

            QDirIterator it("/sys/", QDir::Files, QDirIterator::Subdirectories);
            int fileCount= 0;
            while (it.hasNext()) {
                it.next();
                ++fileCount;
            }
            qDebug() << fileCount << "files found";
            

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            0

            1/5

            30 Sept 2020, 18:43

            • Login

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