Counting file in a directory

本秂侑毒 提交于 2019-12-12 09:40:58

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!