qtqdir

How to fast count large set of files


As I have to process a large number of files, I'd like to show a progress of this process.

I know that iterating using QDirIterator::next() is the best option, but first I need to know the total number of files in a directory (and all its subdirectories).

What is the fastest method to count a large set of files?


Solution

  • Use below code to count all files and directories inside "opt" folder

    QDir dir("/opt/");
    dir.count();
    

    Use below code to list *.jpg files in current and all its subdirectories.

    QDirIterator it("/opt/", QStringList() << "*.jpg", QDir::Files, QDirIterator::Subdirectories);
    int count = 0;
    while (it.hasNext()){
        qDebug() << it.next();
        count++;
    }
    qDebug() << "count:" << count;