How to find Directory Size?
-
wrote on 19 Mar 2020, 06:36 last edited by
How to find directory size(folder size) and display it in MB or GB in Qt.
-
Iterate through all files in the directory with e.g. QDirIterator and sum up the file sizes.
-
Hi
Like @Christian-Ehrlicher says.Example here using QDir
https://stackoverflow.com/questions/47854288/can-not-get-directory-size-in-qt-cQDirIterator Should be faster.
QDirIterator it("c:/vlc", QDirIterator::Subdirectories); qint64 total = 0; while (it.hasNext()) { total += it.fileInfo().size(); } qDebug() << "size" << total;
-
Hi
Like @Christian-Ehrlicher says.Example here using QDir
https://stackoverflow.com/questions/47854288/can-not-get-directory-size-in-qt-cQDirIterator Should be faster.
QDirIterator it("c:/vlc", QDirIterator::Subdirectories); qint64 total = 0; while (it.hasNext()) { total += it.fileInfo().size(); } qDebug() << "size" << total;
wrote on 8 Dec 2023, 03:28 last edited by helsouri 12 Aug 2023, 03:29@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.
-
@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.
wrote on 25 Feb 2025, 09:15 last edited byHi @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 isQDirIterator 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;