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 all files in directory and subdirectories
Forum Updated to NodeBB v4.3 + New Features

Count all files in directory and subdirectories

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 4.6k 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
    Jyang772
    wrote on last edited by
    #1

    Is there a Qt function that counts all files in a directory and all subdirectories? I need to know the total amount of files so that I can create a progressbar as another function processes each file.

    @
    dir.count();
    @
    This only gives me the total amount of files in current directory.

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JoeyAndres
      wrote on last edited by
      #2

      I assume you are using QDir class for variable dir above. Anyway this sounds like a recursive problem. This is possile as long as you guarantee that there are no links. The following is an implementation. Modify it if appropriate.

      @
      int countFilesAndDirRecursive(QString dirName){
      QDir dir(dirName);
      int count = 0;
      QStringList fileNameList = dir.entryList();
      for(QStringList::Iterator iter = fileNameList.begin(); iter != fileNameList.end(); iter++){
      // Test if *iter is directory.
      bool isDir = dir.cd(*iter);
      if(isDir){
      // It is a directory.
      count += countFilesAndDirRecursive(*iter);

         // Change back to parent.
         dir.cdUp();
         continue;
      }
      

      }
      return count;
      }@

      IMPORTANT NOTE: This will guarantee to stop if and only if there are no links(LINUX)/shortcuts(WINDOWS) in your file system. Other than that it should work. There might be some syntax errors since this is typed in my small travel computer.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jyang772
        wrote on last edited by
        #3

        Thanks.
        So I will have to implement counting recursively myself. Will have to put that function on a different thread to keep the GUI responsive.

        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