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 find Directory Size?
Forum Updated to NodeBB v4.3 + New Features

How to find Directory Size?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 5 Posters 3.5k 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.
  • S Offline
    S Offline
    Shankar B
    wrote on last edited by
    #1

    How to find directory size(folder size) and display it in MB or GB in Qt.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Iterate through all files in the directory with e.g. QDirIterator and sum up the file sizes.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        Like @Christian-Ehrlicher says.

        Example here using QDir
        https://stackoverflow.com/questions/47854288/can-not-get-directory-size-in-qt-c

        QDirIterator Should be faster.

            QDirIterator it("c:/vlc", QDirIterator::Subdirectories);
            qint64 total = 0;
            while (it.hasNext()) {        
                total += it.fileInfo().size();
            }
            qDebug() << "size" << total;
        
        
        H 1 Reply Last reply
        4
        • mrjjM mrjj

          Hi
          Like @Christian-Ehrlicher says.

          Example here using QDir
          https://stackoverflow.com/questions/47854288/can-not-get-directory-size-in-qt-c

          QDirIterator Should be faster.

              QDirIterator it("c:/vlc", QDirIterator::Subdirectories);
              qint64 total = 0;
              while (it.hasNext()) {        
                  total += it.fileInfo().size();
              }
              qDebug() << "size" << total;
          
          
          H Offline
          H Offline
          helsouri
          wrote on last edited by helsouri
          #4

          @mrjj sorry i know this topic is old but wanted to first say thank you and this is helpful.

          i belive you missed it.next() in the while loop to actually go to next entry otherwise the loop runs infinitely.

          Additionally might want to add a condition to check that entry is file something like:

          if(it.fileInfo().isFile()) {
          total += it.fileInfo().size();
          }
          

          Another interesting behavior is that the last file in the "main" folder is missed here this is because once the iterator gets to the last file the while check fails so i added a small check after the while to catch the very last file. The entire code becomes:

          QDirIterator it("c:/vlc", QDirIterator::Subdirectories);
          qint64 total = 0;
          while (it.hasNext()) { 
              // check if entry is file 
              if(it.fileInfo().isFile()) {
                  total += it.fileInfo().size();
              }
              it.next();
          }
          
          // if there is a file left "at the end" get it's size
          if(it.fileInfo().isFile()) {
              total+= it.fileInfo().size();
          }
          
          qDebug() << "size" << total;
          

          I know i am late to the party but this was my solution.

          7 1 Reply Last reply
          1
          • H helsouri

            @mrjj sorry i know this topic is old but wanted to first say thank you and this is helpful.

            i belive you missed it.next() in the while loop to actually go to next entry otherwise the loop runs infinitely.

            Additionally might want to add a condition to check that entry is file something like:

            if(it.fileInfo().isFile()) {
            total += it.fileInfo().size();
            }
            

            Another interesting behavior is that the last file in the "main" folder is missed here this is because once the iterator gets to the last file the while check fails so i added a small check after the while to catch the very last file. The entire code becomes:

            QDirIterator it("c:/vlc", QDirIterator::Subdirectories);
            qint64 total = 0;
            while (it.hasNext()) { 
                // check if entry is file 
                if(it.fileInfo().isFile()) {
                    total += it.fileInfo().size();
                }
                it.next();
            }
            
            // if there is a file left "at the end" get it's size
            if(it.fileInfo().isFile()) {
                total+= it.fileInfo().size();
            }
            
            qDebug() << "size" << total;
            

            I know i am late to the party but this was my solution.

            7 Offline
            7 Offline
            7erg
            wrote on last edited by
            #5

            Hi @helsouri !
            Take a look at usages here https://doc.qt.io/qt-6/qdiriterator.html
            They told After construction, the iterator is located before the first directory entry
            So the proper code is

            QDirIterator it("c:/vlc", QDirIterator::Subdirectories);
            qint64 total = 0;
            while (it.hasNext()) { 
                it.next();
                // check if entry is file 
                if(it.fileInfo().isFile()) {
                    total += it.fileInfo().size();
                }
            }
            qDebug() << "size" << total;
            
            1 Reply Last reply
            1

            • Login

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