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. Counting files in a directory

Counting files in a directory

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 697 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.
  • T Offline
    T Offline
    tdc0311
    wrote on last edited by
    #1

    Hi so i'm working on a desktop project in Qt, i want to count all the files in a directory or a folder choosen, the method below is used for counting files, but somehow it does not run correctly, i've try several ways to fix it but it still give the same result, my method works well on folders without hidden files, but with the folders that have hidden files it doesn't return the correct number of files in that directory.

     if (path.isEmpty()) {
            qWarning() << "Empty path provided.";
            return 0;
        }
    
        // Check if the path exists and is a directory
        QFileInfo pathInfo(path);
        if (!pathInfo.exists() || !pathInfo.isDir()) {
            qWarning() << "Provided path does not exist or is not a directory.";
            return 0;
        }
    
        int totalFiless = 0;
        QQueue<QString> directoryQueue;
        directoryQueue.enqueue(path);
        QSet<QString> visitedDirectories;  // To handle symlinks and avoid infinite loops
    
        while (!directoryQueue.isEmpty()) {
            QString currentPath = directoryQueue.dequeue();
    
            // Skip already visited directories
            if (visitedDirectories.contains(currentPath)) {
                continue;
            }
            visitedDirectories.insert(currentPath);
    
            QDir dir(currentPath);
            QFileInfoList entries = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::Hidden | QDir::System | QDir::NoDotAndDotDot);
    
            for (const QFileInfo &entry : entries) {
                if (entry.isFile()) {
                    totalFiless++;
                } else if (entry.isDir()) {
                    directoryQueue.enqueue(entry.filePath());
                }
            }
        }
        return totalFiless;
    
    jsulmJ JonBJ 2 Replies Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      It should work. May be you can check with only one directory & trouble shoot the issue.
      How much is the mis-match ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      1
      • T tdc0311

        Hi so i'm working on a desktop project in Qt, i want to count all the files in a directory or a folder choosen, the method below is used for counting files, but somehow it does not run correctly, i've try several ways to fix it but it still give the same result, my method works well on folders without hidden files, but with the folders that have hidden files it doesn't return the correct number of files in that directory.

         if (path.isEmpty()) {
                qWarning() << "Empty path provided.";
                return 0;
            }
        
            // Check if the path exists and is a directory
            QFileInfo pathInfo(path);
            if (!pathInfo.exists() || !pathInfo.isDir()) {
                qWarning() << "Provided path does not exist or is not a directory.";
                return 0;
            }
        
            int totalFiless = 0;
            QQueue<QString> directoryQueue;
            directoryQueue.enqueue(path);
            QSet<QString> visitedDirectories;  // To handle symlinks and avoid infinite loops
        
            while (!directoryQueue.isEmpty()) {
                QString currentPath = directoryQueue.dequeue();
        
                // Skip already visited directories
                if (visitedDirectories.contains(currentPath)) {
                    continue;
                }
                visitedDirectories.insert(currentPath);
        
                QDir dir(currentPath);
                QFileInfoList entries = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::Hidden | QDir::System | QDir::NoDotAndDotDot);
        
                for (const QFileInfo &entry : entries) {
                    if (entry.isFile()) {
                        totalFiless++;
                    } else if (entry.isDir()) {
                        directoryQueue.enqueue(entry.filePath());
                    }
                }
            }
            return totalFiless;
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @tdc0311 said in Counting files in a directory:

        it doesn't return the correct number of files in that directory

        What does it mean exactly? It does not count hidden files?
        Are there files which are not accessible by the user?
        Did you print out file names to see which files are not counted?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          If I may suggest a much, much simpler function to accomplish this:

          QStringList findFilesInDirectory(const QString &directoryPath) {
              QStringList fileList;
              QDirIterator it(directoryPath, QDir::Files | QDir::Hidden, QDirIterator::Subdirectories);
          
              while (it.hasNext()) {
                  it.next();
                  fileList.append(it.filePath());
              }
          
              return fileList;
          }
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          4
          • T tdc0311

            Hi so i'm working on a desktop project in Qt, i want to count all the files in a directory or a folder choosen, the method below is used for counting files, but somehow it does not run correctly, i've try several ways to fix it but it still give the same result, my method works well on folders without hidden files, but with the folders that have hidden files it doesn't return the correct number of files in that directory.

             if (path.isEmpty()) {
                    qWarning() << "Empty path provided.";
                    return 0;
                }
            
                // Check if the path exists and is a directory
                QFileInfo pathInfo(path);
                if (!pathInfo.exists() || !pathInfo.isDir()) {
                    qWarning() << "Provided path does not exist or is not a directory.";
                    return 0;
                }
            
                int totalFiless = 0;
                QQueue<QString> directoryQueue;
                directoryQueue.enqueue(path);
                QSet<QString> visitedDirectories;  // To handle symlinks and avoid infinite loops
            
                while (!directoryQueue.isEmpty()) {
                    QString currentPath = directoryQueue.dequeue();
            
                    // Skip already visited directories
                    if (visitedDirectories.contains(currentPath)) {
                        continue;
                    }
                    visitedDirectories.insert(currentPath);
            
                    QDir dir(currentPath);
                    QFileInfoList entries = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::Hidden | QDir::System | QDir::NoDotAndDotDot);
            
                    for (const QFileInfo &entry : entries) {
                        if (entry.isFile()) {
                            totalFiless++;
                        } else if (entry.isDir()) {
                            directoryQueue.enqueue(entry.filePath());
                        }
                    }
                }
                return totalFiless;
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @tdc0311
            In addition to the other two replies.

            I cannot see what any particular issue might be with hidden files. Your code should count them.

            Are you aware that (if I understand the code right, untested and I don't use Windows) you count all descendent directory, recursively, contents too?

                        } else if (entry.isDir()) {
                            directoryQueue.enqueue(entry.filePath());
                        }
            

            Is that what you intend? If you have a subdirectory which is hidden its recursive contents would be included, is that the cause of the mismatch?

            @J-Hilk's suggested function is indeed a lot simpler. I am unsure of two items of behaviour, which you might like to check:

            • QDir::Files | QDir::Hidden: I am unsure whether this would include a hidden directory (as well as hidden files), or only files which are hidden.
            • QDirIterator::Subdirectories: I am unsure whether this means only immediate sub-directories of the directory path or all, recursive sub-directories.

            Obviously you must decide whether you want your "count" to include sub-directories recursively or not.

            J.HilkJ 1 Reply Last reply
            0
            • JonBJ JonB

              @tdc0311
              In addition to the other two replies.

              I cannot see what any particular issue might be with hidden files. Your code should count them.

              Are you aware that (if I understand the code right, untested and I don't use Windows) you count all descendent directory, recursively, contents too?

                          } else if (entry.isDir()) {
                              directoryQueue.enqueue(entry.filePath());
                          }
              

              Is that what you intend? If you have a subdirectory which is hidden its recursive contents would be included, is that the cause of the mismatch?

              @J-Hilk's suggested function is indeed a lot simpler. I am unsure of two items of behaviour, which you might like to check:

              • QDir::Files | QDir::Hidden: I am unsure whether this would include a hidden directory (as well as hidden files), or only files which are hidden.
              • QDirIterator::Subdirectories: I am unsure whether this means only immediate sub-directories of the directory path or all, recursive sub-directories.

              Obviously you must decide whether you want your "count" to include sub-directories recursively or not.

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @JonB said in Counting files in a directory:

              QDir::Files | QDir::Hidden: I am unsure whether this would include a hidden directory (as well as hidden files), or only files which are hidden.

              since the filter is set to files only, hidden folders will be ignored

              @JonB said in Counting files in a directory:

              QDirIterator::Subdirectories: I am unsure whether this means only immediate sub-directories of the directory path or all, recursive sub-directories.

              all, recursively


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              2

              • Login

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