-1

hi i use Qt and i want to know how much file are in a folder (include subfolder) i find some code in internet but all of them only count file in a folder and not its subfolder
like this codes

#include <stdio.h>
#include <dirent.h>

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        printf("Usage: ./count \"<path>\"\n");
        return 1;
    }

    struct dirent *de;
    DIR *dir = opendir(argv[1]);
    if(!dir)
    {
        printf("opendir() failed! Does it exist?\n");
        return 1;
    }

    unsigned long count=0;
        while(de = readdir(dir))
     {
          ++count;
     }

    closedir(dir);
    printf("%lu\n", count);

    return 0;
}

i find this code here, it only count file in a folder and not its subfoler
anyone can help me
edit 1
i test this code from this page, it working but it can edit for less cpu usage?

  • In 2020 you probably want to start here instead: [https://en.cppreference.com/w/cpp/filesystem](https://en.cppreference.com/w/cpp/filesystem) specifically [https://en.cppreference.com/w/cpp/filesystem/recursive_directory_iterator](https://en.cppreference.com/w/cpp/filesystem/recursive_directory_iterator) – drescherjm Sep 30 '20 at 17:41
  • Since you tagged for the Qt framework, there are ways to do this using `Qt` : [https://doc.qt.io/qt-5/qdiriterator.html](https://doc.qt.io/qt-5/qdiriterator.html) – drescherjm Sep 30 '20 at 17:42
  • You mention Qt and tagged as `C++` but your code looks more like `C` than `C++`. So maybe you should clarify if you want to list files in `C`, `C++` with the standard library or `C++` with `Qt`. Because that's different questions with different answers – Antoine Sep 30 '20 at 18:42
  • @Antoine my code is C++, and also i use Qt, im looking for best way (and faster way) to do this job, and I prefer to dont use any external library like Boost, only standard C++ library and Qt module. Sorry I did not clarify in the first post – code-tester Sep 30 '20 at 18:49
  • As @drescherjm mentionned, file listing is supported natively in the modern C++ standard library. I still think you aren't 100% clear on C vs C++. Since C++ is mostly backwards compatible with C it's not obvious, but using `printf` and `struct dirent *de;` is more `C` than `C++`. – Antoine Sep 30 '20 at 18:54
  • @Antoine as i said above, the code i put in first post is just a example i found on some site, and im not using it, i just put this code here so i can tell what im want – code-tester Sep 30 '20 at 19:00

2 Answers2

2

The following code runs for Qt.

QStringList nameFilters;
nameFilters << "*.jpg";
QDirIterator it(QDir::currentPath(), nameFilters, QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()) {
    qDebug() << it.next();
}

use nameFilters << "*";if you want to know all the files in the parent directory and its sub-directories.

smalik
  • 343
  • 4
  • 9
  • this code print all file name, and if i delete `qDebug() << it.next();` and add something like file++ for know how many file are in the folder its just make a big loop – code-tester Sep 30 '20 at 18:29
  • 1
    don't delete the `it.next();` part otherwise it won't advance. – drescherjm Sep 30 '20 at 19:03
2

sumit_smk code is ok but its need a little edit for your case
here is the code you can use

int file = 0;
QDirIterator it("/Your_folder/", QDir::Files, QDirIterator::Subdirectories);
    while (it.hasNext())
    {
        if(it.next() > 0 )
        {
            file++;
        }
    }
    qDebug() << file;
sanab3343
  • 154
  • 1
  • 11