Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. problem in reading a size of a Dir

problem in reading a size of a Dir

Scheduled Pinned Locked Moved Solved Mobile and Embedded
12 Posts 4 Posters 4.0k Views 2 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.
  • B Offline
    B Offline
    benamiar
    wrote on last edited by
    #1

    Hello everyone i have a problem to read the size of my folder te size displayed doesn't correspond to the global size i think my prgram don't add the size of folders he just calculate the size of files. thanks for help.

    qint64 dirSize(QString dirPath)
    {
        qint64 size = 0;
        QDir dir(dirPath);
    
        //calculate total size of current directories' files
        QDir::Filters fileFilters = QDir::Files|QDir::System|QDir::Hidden;
        for(QString filePath : dir.entryList(fileFilters)) {
            QFileInfo fi(dir, filePath);
            size+= fi.size();
        }
        //add size of child directories recursively
        QDir::Filters dirFilters = QDir::Dirs|QDir::NoDotAndDotDot|QDir::System|QDir::Hidden;
        for(QString childDirPath : dir.entryList(dirFilters))
            size+= dirSize(dirPath + QDir::separator() + childDirPath);
        return size;
    }
    

    sorry for my bad english

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2
      • Use QDirIterator, it is faster
      • there is no need to use QDir::separator() in Qt calls, just use unix path notation - /
      • directory size is usually just a few bytes, so your overall size should not be a lot off from what your operating system tells you. Are the numbers totally different or just a bit?
      • in your second loop, use entryInfoList() and add size to your sum, then continue with size += dirSize() like now
      • lastly, though - why not use your Operating System to do the job? It will be far more reliable (will take symlinks, OS-specific folders and files into account)

      (Z(:^

      B 1 Reply Last reply
      2
      • Shrinidhi UpadhyayaS Offline
        Shrinidhi UpadhyayaS Offline
        Shrinidhi Upadhyaya
        wrote on last edited by
        #3

        Hi @benamiar , what is the size which is getting displayed in the program and what is your actual size?

        • One thing, in the dirSize function it goes through all the files and the folders recursively, so if your directory is very big it will take some time.

        • Just create a simple qt project and add the sample code:-

        Sample Code:-

        qint64 DummyClass:: dirSize(QString dirPath) {
        qint64 size = 0;
        QDir dir(dirPath);
        
        QDir::Filters fileFilters = QDir::Files|QDir::System|QDir::Hidden;
        for(QString filePath : dir.entryList(fileFilters)) {
            QFileInfo fi(dir, filePath);
            size+= fi.size();
        }
        
        QDir::Filters dirFilters = QDir::Dirs|QDir::NoDotAndDotDot|QDir::System|QDir::Hidden;
        for(QString childDirPath : dir.entryList(dirFilters))
            size+= dirSize(dirPath + QDir::separator() + childDirPath);
        return size;
        }
        
        QString DummyClass::formatSize(qint64 size) {
        QStringList units = {"Bytes", "KB", "MB", "GB", "TB", "PB"};
        int i;
        double outputSize = size;
        for(i=0; i<units.size()-1; i++) {
            if(outputSize<1024) break;
            outputSize= outputSize/1024;
        }
        return QString("%0 %1").arg(outputSize, 0, 'f', 2).arg(units[i]);
        }
        
        void DummyClass::dummyFunc()
        {
        QString directoryPath = QFileDialog
                ::getExistingDirectory(nullptr, "Choose directory: ", "");
        //####Printing the path
        qDebug() << "Directory Path" << directoryPath;
        qint64 size = dirSize(directoryPath);
        //####Printing the size
        qDebug() << "Size" << formatSize(size);
        }
        

        Note:- You need to call dummyFunc(), so you can call it anywhere like just inside a constructor or on a click of button.

        So once you execute this code, you will get a fileDialog like this:-

        0_1559558188129_362011fa-e0d9-4cbd-b170-b00cd9a8ac25-image.png

        So you can just select any directory you want and click on "Select Folder"

        Once you do that you will get the path and the size printed on your console like this:-

        0_1559558290705_ac3213ba-0298-4cc8-bc42-afb4ff8ca701-image.png

        Note:- As it is recursive, just you can select a folder which is small in size and confirm its size by checking what is printed on the console and then by right clicking on the folder->Properties->Size.

        Shrinidhi Upadhyaya.
        Upvote the answer(s) that helped you to solve the issue.

        1 Reply Last reply
        3
        • B Offline
          B Offline
          benamiar
          wrote on last edited by
          #4

          tnx for respond the size displayed in my linux system is 604KB and the program say that i have 546 KB it's a difference of 60KB.

          1 Reply Last reply
          0
          • sierdzioS sierdzio
            • Use QDirIterator, it is faster
            • there is no need to use QDir::separator() in Qt calls, just use unix path notation - /
            • directory size is usually just a few bytes, so your overall size should not be a lot off from what your operating system tells you. Are the numbers totally different or just a bit?
            • in your second loop, use entryInfoList() and add size to your sum, then continue with size += dirSize() like now
            • lastly, though - why not use your Operating System to do the job? It will be far more reliable (will take symlinks, OS-specific folders and files into account)
            B Offline
            B Offline
            benamiar
            wrote on last edited by
            #5

            @sierdzio i have more than one folder so 4 KB for each folder want be added to the size.

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              @benamiar said in problem in reading a size of a Dir:

              QDir::Filters dirFilters = QDir::Dirs|QDir::NoDotAndDotDot|QDir::System|QDir::Hidden;
              for(QString childDirPath : dir.entryList(dirFilters))
              size+= dirSize(dirPath + QDir::separator() + childDirPath);

              Like I said then:

              QDir::Filters dirFilters = QDir::Dirs|QDir::NoDotAndDotDot|QDir::System|QDir::Hidden;
              const auto &list = dir.entryInfoList(dirFilters);
              for(const QFileInfo &dir : list) {
                size += dir.size();
                size+= dirSize(dirPath + "/" + dir.filename());
              }
              

              (untested)

              (Z(:^

              B 1 Reply Last reply
              0
              • sierdzioS sierdzio

                @benamiar said in problem in reading a size of a Dir:

                QDir::Filters dirFilters = QDir::Dirs|QDir::NoDotAndDotDot|QDir::System|QDir::Hidden;
                for(QString childDirPath : dir.entryList(dirFilters))
                size+= dirSize(dirPath + QDir::separator() + childDirPath);

                Like I said then:

                QDir::Filters dirFilters = QDir::Dirs|QDir::NoDotAndDotDot|QDir::System|QDir::Hidden;
                const auto &list = dir.entryInfoList(dirFilters);
                for(const QFileInfo &dir : list) {
                  size += dir.size();
                  size+= dirSize(dirPath + "/" + dir.filename());
                }
                

                (untested)

                B Offline
                B Offline
                benamiar
                wrote on last edited by
                #7

                @sierdzio even with your sollution i still have like 14KB lost :p

                sierdzioS 1 Reply Last reply
                0
                • B benamiar

                  @sierdzio even with your sollution i still have like 14KB lost :p

                  sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #8

                  @benamiar said in problem in reading a size of a Dir:

                  @sierdzio even with your sollution i still have like 14KB lost :p

                  OK, at least it's a bit better :D Are there any symlinks inside the directory? Perhaps they are changing the result.

                  But ultimately, it is very possible you will never get the same result. I've noticed many times for example, that the same file will show different size on Linux that on Windows, it is possible a similar effect is showing up here.

                  (Z(:^

                  1 Reply Last reply
                  0
                  • Pradeep P NP Offline
                    Pradeep P NP Offline
                    Pradeep P N
                    wrote on last edited by Pradeep P N
                    #9

                    Hi @benamiar

                    The Qt Code provided by @Shrinidhi-Upadhyaya will give you complete size of the Folder including the hidden files (dot files)

                    • Please verify the size of the file using the command prompt to get actual data size of the file.

                    • I Guess The size given in File->Properties (in Ubuntu) is the size on the disk not the actual size of the folder contents.

                    Below are few images you can check (Ubuntu 16.04)

                    from Terminal
                    0_1559627302802_ad396ec1-a587-426b-ad47-fd7de6fe148c-image.png

                    from Properties
                    0_1559627321781_28880f41-861c-43d4-a104-1847926fbb85-image.png

                    the Qt Output from @Shrinidhi-Upadhyaya code.

                    Directory Path "/home/daimler/Desktop/emptyFolder"
                    Size "470.64 KB"
                    

                    I also tested the same on Windows.

                    The size from properties is
                    0_1559628152727_2f7ebd14-cc41-4e0a-bb23-9dab0bd61a16-image.png
                    Qt Output is :

                    Starting C:\Users\pnimbal\Desktop\build-DirSize-Desktop_Qt_5_9_2_MinGW_32bit-Debug\debug\DirSize.exe...
                    Directory Path "C:/Users/pnimbal/Desktop/test"
                    Size "4.60 KB"
                    
                    
                    Directory Path "C:/Users/pnimbal/Documents/-------/---------"
                    Size "733.99 KB"
                    

                    Pradeep Nimbalkar.
                    Upvote the answer(s) that helped you to solve the issue...
                    Keep code clean.

                    B 1 Reply Last reply
                    2
                    • sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on last edited by sierdzio
                      #10

                      @Pradeep-P-N said in problem in reading a size of a Dir:

                      The Qt Code provided by @Shrinidhi-Upadhyaya will give you complete size of the Folder including the hidden files (dot files)

                      It is exactly the same code that OP has posted in his initial question...

                      (Z(:^

                      1 Reply Last reply
                      0
                      • Pradeep P NP Pradeep P N

                        Hi @benamiar

                        The Qt Code provided by @Shrinidhi-Upadhyaya will give you complete size of the Folder including the hidden files (dot files)

                        • Please verify the size of the file using the command prompt to get actual data size of the file.

                        • I Guess The size given in File->Properties (in Ubuntu) is the size on the disk not the actual size of the folder contents.

                        Below are few images you can check (Ubuntu 16.04)

                        from Terminal
                        0_1559627302802_ad396ec1-a587-426b-ad47-fd7de6fe148c-image.png

                        from Properties
                        0_1559627321781_28880f41-861c-43d4-a104-1847926fbb85-image.png

                        the Qt Output from @Shrinidhi-Upadhyaya code.

                        Directory Path "/home/daimler/Desktop/emptyFolder"
                        Size "470.64 KB"
                        

                        I also tested the same on Windows.

                        The size from properties is
                        0_1559628152727_2f7ebd14-cc41-4e0a-bb23-9dab0bd61a16-image.png
                        Qt Output is :

                        Starting C:\Users\pnimbal\Desktop\build-DirSize-Desktop_Qt_5_9_2_MinGW_32bit-Debug\debug\DirSize.exe...
                        Directory Path "C:/Users/pnimbal/Desktop/test"
                        Size "4.60 KB"
                        
                        
                        Directory Path "C:/Users/pnimbal/Documents/-------/---------"
                        Size "733.99 KB"
                        
                        B Offline
                        B Offline
                        benamiar
                        wrote on last edited by benamiar
                        #11

                        @Pradeep-P-N okey i understand now thnaks for yout clarification

                        Pradeep P NP 1 Reply Last reply
                        0
                        • B benamiar

                          @Pradeep-P-N okey i understand now thnaks for yout clarification

                          Pradeep P NP Offline
                          Pradeep P NP Offline
                          Pradeep P N
                          wrote on last edited by Pradeep P N
                          #12

                          @benamiar
                          Cool. If it's resolved you can make the ticket as Solved.

                          Good luck.

                          Pradeep Nimbalkar.
                          Upvote the answer(s) that helped you to solve the issue...
                          Keep code clean.

                          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