Count File in a folder (include subfolder)
Solved
General and Desktop
-
hi 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; }
what is best and faster way to count all file in a folder and its subfolders?
thanks -
Hi and welcome to devnet,
Since you are using Qt, you should check QDirIterator.
-
@SGaist is this code right?
it workingQDirIterator it("/sys/", QDir::Files, QDirIterator::Subdirectories); while (it.hasNext()) { if(it.next() > 0 ) { howfile++; } } qDebug() << howfile;
i first try to just delete
it.next()
but its make while infinite -
Looks correct yes
-
@jackventor said in Count File in a folder (include subfolder):
is this code right?
I don't think so.
QDirIterator::next()
is returning a QString containing file name, it is not a boolean!This looks better to me:
QDirIterator it("/sys/", QDir::Files, QDirIterator::Subdirectories); int fileCount= 0; while (it.hasNext()) { it.next(); ++fileCount; } qDebug() << fileCount << "files found";