Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through only certain type of files using QDirIterator

Tags:

c++

qt

I want to iterate through only .xml files (all files under selected folder and its sub-directories), is that possible with QDirIterator?

QDirIterator iter( rootDir, QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);

QString fileName;

while(iter.hasNext() )
{
   qDebug() << iter.next();

   fileName = iter.fileName();

   // now I have to check if fileName is indeed a .xml extension
}

As can be seen in code above, if my iterator can jump to .xml files only than I don't have to check for file extension in the loop..is it possible?

like image 453
zar Avatar asked Feb 11 '15 19:02

zar


1 Answers

One of the constructors of QDirIterator allows a nameFilters argument:

QDirIterator::QDirIterator ( const QString & path, const QStringList & nameFilters, QDir::Filters filters = QDir::NoFilter, IteratorFlags flags = NoIteratorFlags )

Constructs a QDirIterator that can iterate over path, using nameFilters and filters.

The nameFilters argument is not properly documented, but there is a good chance it works like in QDir::setNameFilters.

QDirIterator it(rootdir, QStringList() << "*.xml", QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()) {
    qDebug() << it.next();
}
like image 94
Smasho Avatar answered Sep 28 '22 10:09

Smasho