Counting file in a directory

2019-05-17 17:46发布

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

标签: c++ qt qt4
5条回答
走好不送
2楼-- · 2019-05-17 18:05

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.

查看更多
迷人小祖宗
3楼-- · 2019-05-17 18:06

You'll need exclude . and .. - QDir::Files filter can help you there.

Relevant docs:

查看更多
SAY GOODBYE
4楼-- · 2019-05-17 18:18

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);
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-05-17 18:20

You should use flags QDir::Filters with QDir::NoDotAndDotDot

查看更多
冷血范
6楼-- · 2019-05-17 18:23

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();
查看更多
登录 后发表回答