Counting files in a directory
-
Hi so i'm working on a desktop project in Qt, i want to count all the files in a directory or a folder choosen, the method below is used for counting files, but somehow it does not run correctly, i've try several ways to fix it but it still give the same result, my method works well on folders without hidden files, but with the folders that have hidden files it doesn't return the correct number of files in that directory.
if (path.isEmpty()) { qWarning() << "Empty path provided."; return 0; } // Check if the path exists and is a directory QFileInfo pathInfo(path); if (!pathInfo.exists() || !pathInfo.isDir()) { qWarning() << "Provided path does not exist or is not a directory."; return 0; } int totalFiless = 0; QQueue<QString> directoryQueue; directoryQueue.enqueue(path); QSet<QString> visitedDirectories; // To handle symlinks and avoid infinite loops while (!directoryQueue.isEmpty()) { QString currentPath = directoryQueue.dequeue(); // Skip already visited directories if (visitedDirectories.contains(currentPath)) { continue; } visitedDirectories.insert(currentPath); QDir dir(currentPath); QFileInfoList entries = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::Hidden | QDir::System | QDir::NoDotAndDotDot); for (const QFileInfo &entry : entries) { if (entry.isFile()) { totalFiless++; } else if (entry.isDir()) { directoryQueue.enqueue(entry.filePath()); } } } return totalFiless;
-
It should work. May be you can check with only one directory & trouble shoot the issue.
How much is the mis-match ? -
Hi so i'm working on a desktop project in Qt, i want to count all the files in a directory or a folder choosen, the method below is used for counting files, but somehow it does not run correctly, i've try several ways to fix it but it still give the same result, my method works well on folders without hidden files, but with the folders that have hidden files it doesn't return the correct number of files in that directory.
if (path.isEmpty()) { qWarning() << "Empty path provided."; return 0; } // Check if the path exists and is a directory QFileInfo pathInfo(path); if (!pathInfo.exists() || !pathInfo.isDir()) { qWarning() << "Provided path does not exist or is not a directory."; return 0; } int totalFiless = 0; QQueue<QString> directoryQueue; directoryQueue.enqueue(path); QSet<QString> visitedDirectories; // To handle symlinks and avoid infinite loops while (!directoryQueue.isEmpty()) { QString currentPath = directoryQueue.dequeue(); // Skip already visited directories if (visitedDirectories.contains(currentPath)) { continue; } visitedDirectories.insert(currentPath); QDir dir(currentPath); QFileInfoList entries = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::Hidden | QDir::System | QDir::NoDotAndDotDot); for (const QFileInfo &entry : entries) { if (entry.isFile()) { totalFiless++; } else if (entry.isDir()) { directoryQueue.enqueue(entry.filePath()); } } } return totalFiless;
@tdc0311 said in Counting files in a directory:
it doesn't return the correct number of files in that directory
What does it mean exactly? It does not count hidden files?
Are there files which are not accessible by the user?
Did you print out file names to see which files are not counted? -
If I may suggest a much, much simpler function to accomplish this:
QStringList findFilesInDirectory(const QString &directoryPath) { QStringList fileList; QDirIterator it(directoryPath, QDir::Files | QDir::Hidden, QDirIterator::Subdirectories); while (it.hasNext()) { it.next(); fileList.append(it.filePath()); } return fileList; }
-
Hi so i'm working on a desktop project in Qt, i want to count all the files in a directory or a folder choosen, the method below is used for counting files, but somehow it does not run correctly, i've try several ways to fix it but it still give the same result, my method works well on folders without hidden files, but with the folders that have hidden files it doesn't return the correct number of files in that directory.
if (path.isEmpty()) { qWarning() << "Empty path provided."; return 0; } // Check if the path exists and is a directory QFileInfo pathInfo(path); if (!pathInfo.exists() || !pathInfo.isDir()) { qWarning() << "Provided path does not exist or is not a directory."; return 0; } int totalFiless = 0; QQueue<QString> directoryQueue; directoryQueue.enqueue(path); QSet<QString> visitedDirectories; // To handle symlinks and avoid infinite loops while (!directoryQueue.isEmpty()) { QString currentPath = directoryQueue.dequeue(); // Skip already visited directories if (visitedDirectories.contains(currentPath)) { continue; } visitedDirectories.insert(currentPath); QDir dir(currentPath); QFileInfoList entries = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::Hidden | QDir::System | QDir::NoDotAndDotDot); for (const QFileInfo &entry : entries) { if (entry.isFile()) { totalFiless++; } else if (entry.isDir()) { directoryQueue.enqueue(entry.filePath()); } } } return totalFiless;
@tdc0311
In addition to the other two replies.I cannot see what any particular issue might be with hidden files. Your code should count them.
Are you aware that (if I understand the code right, untested and I don't use Windows) you count all descendent directory, recursively, contents too?
} else if (entry.isDir()) { directoryQueue.enqueue(entry.filePath()); }
Is that what you intend? If you have a subdirectory which is hidden its recursive contents would be included, is that the cause of the mismatch?
@J-Hilk's suggested function is indeed a lot simpler. I am unsure of two items of behaviour, which you might like to check:
QDir::Files | QDir::Hidden
: I am unsure whether this would include a hidden directory (as well as hidden files), or only files which are hidden.QDirIterator::Subdirectories
: I am unsure whether this means only immediate sub-directories of the directory path or all, recursive sub-directories.
Obviously you must decide whether you want your "count" to include sub-directories recursively or not.
-
@tdc0311
In addition to the other two replies.I cannot see what any particular issue might be with hidden files. Your code should count them.
Are you aware that (if I understand the code right, untested and I don't use Windows) you count all descendent directory, recursively, contents too?
} else if (entry.isDir()) { directoryQueue.enqueue(entry.filePath()); }
Is that what you intend? If you have a subdirectory which is hidden its recursive contents would be included, is that the cause of the mismatch?
@J-Hilk's suggested function is indeed a lot simpler. I am unsure of two items of behaviour, which you might like to check:
QDir::Files | QDir::Hidden
: I am unsure whether this would include a hidden directory (as well as hidden files), or only files which are hidden.QDirIterator::Subdirectories
: I am unsure whether this means only immediate sub-directories of the directory path or all, recursive sub-directories.
Obviously you must decide whether you want your "count" to include sub-directories recursively or not.
@JonB said in Counting files in a directory:
QDir::Files | QDir::Hidden: I am unsure whether this would include a hidden directory (as well as hidden files), or only files which are hidden.
since the filter is set to files only, hidden folders will be ignored
@JonB said in Counting files in a directory:
QDirIterator::Subdirectories: I am unsure whether this means only immediate sub-directories of the directory path or all, recursive sub-directories.
all, recursively