Reputation: 3431
I want to count number of file in a directory, I used count method in QDir class but it always return number of file plus two! why does it do this work ? thanks
Upvotes: 5
Views: 10907
Reputation: 174
You Can use :
QFileInfo fileInfo(m_logFilePath);
QDir dir(fileInfo.absoluteDir());
QStringList totalfiles;
totalfiles = dir.entryList(QStringList("*"), QDir::Files | QDir::NoSymLinks);
using file name
totalfiles = dir.entryList(QStringList("filename"), QDir::Files | QDir::Names);
Upvotes: 0
Reputation: 187994
You'll need exclude .
and ..
- QDir::Files
filter can help you there.
Relevant docs:
Upvotes: 5
Reputation: 12227
I am posting a complete answer.
QString path = "c:\test"; // assume it is some path
QDir dir( path );
dir.setFilter( QDir::AllEntries | QDir::NoDotAndDotDot );
int total_files = dir.count();
Upvotes: 12
Reputation: 206659
QDir.count()
returns the total count of files and directories in the directory. This includes the .
(this) and ..
(parent) directory entries. So the count is always two more than the "real" files and subdirectories.
Upvotes: 13