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);