Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

problem in reading a size of a Dir



  • 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


  • Moderators

    • 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)


  • 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.



  • 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.



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


  • Moderators

    @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)



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


  • Moderators

    @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.



  • 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"
    

  • Moderators

    @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...



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



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

    Good luck.


Log in to reply