问题
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
回答1:
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.
回答2:
You should use flags QDir::Filters with QDir::NoDotAndDotDot
回答3:
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();
回答4:
You'll need exclude . and .. - QDir::Files filter can help you there.
Relevant docs:
- http://doc.qt.io/qt-4.8/qdir.html#entryList
- http://doc.qt.io/qt-4.8/qdir.html#Filter-enum
回答5:
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);
来源:https://stackoverflow.com/questions/6890757/counting-file-in-a-directory