How to find Directory Size?
-
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;
@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.
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 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;