amiref
amiref

Reputation: 3431

Counting file in a directory

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

Answers (5)

Varun Kumar
Varun Kumar

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

miku
miku

Reputation: 187994

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

Relevant docs:

Upvotes: 5

zar
zar

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

Alok Save
Alok Save

Reputation: 206508

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

Upvotes: 14

Mat
Mat

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

Related Questions