PDA

View Full Version : List all XML files in a directory



ouekah
29th March 2010, 21:47
i would like to read all XML files of a directory. To this end I use the entryList function of QDir.

I have seen that you precise a filter to display files only, but is there a way to be more precise and display only .xml documents ?

For the moment I use the following instruction:



QStringList entries = dir.entryList(QDir::Files);

toutarrive
29th March 2010, 22:36
Use that function:
QDir::entryList ( const QStringList & nameFilters, Filters filters = NoFilter, SortFlags sort = NoSort ) const
Simple example:


QDir dir(yourPath);
QStringList filters;
filters << "*.xml"; //append your xml filter. You can add other filters here

foreach ( QString file, dir.entryList(filters, QDir::Files) )
// do wathever with *.xml files

Have look as well at http://doc.trolltech.com/4.6/qdir.html#setNameFilters

MaliciousMatrix
30th August 2015, 05:04
How do I get the current file name and path within the foreach loop? I need to draw information from each one and put it into a vector.


QDir dir(path);
QStringList filters;
filters << "*.txt";
foreach(QString file, dir.entryList(filters, QDir::Files)){
QFile mine(/*file path? */);
mine.open(QIODevice::ReadOnly);
// stuff here
}

anbu01
30th August 2015, 07:03
For file name and path use QFileInfo class.

anda_skoa
30th August 2015, 08:47
For file name and path use QFileInfo class.

Exactly. Loop over entryInfoList() to get these.

Cheers,
_