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?
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With