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 Updated to NodeBB v4.3 + New Features

Count File in a folder (include subfolder)

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 652 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.
  • J Offline
    J Offline
    jackventor
    wrote on 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
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 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
      2
      • SGaistS SGaist

        Hi and welcome to devnet,

        Since you are using Qt, you should check QDirIterator.

        J Offline
        J Offline
        jackventor
        wrote on 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

        KroMignonK 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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

            @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

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by KroMignon
            #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

            • Login

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