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
-
- 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 withsize += 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:-
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:-
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.
-
-
@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)
-
@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
from Properties
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
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-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