PDA

View Full Version : Searching files from directories & its subdirectories



ashukla
27th October 2007, 09:02
Hi All,

I am trying to search files from directories & its subdirectories. I want to do recursive searching. I am unable to find option to do it.
Please tell me the way to do recursive searching using QFileDialog.

Thanks in advance!

momesana
27th October 2007, 12:17
You have to get the entries of a directory and then iterate over every single file and check whether it is a directory itself. If that is true, you recursively start the function on that file. Take a look at the entryList() and entryInfoList() members of QDir.

jpn
27th October 2007, 12:51
See this post for how to recursively iterate through a directory and its subdirectories: http://www.qtcentre.org/forum/p-recursive-removal-of-dirs-post45687/postcount2.html. Alternatively, you could abuse QDirModel and QAbstractItemModel::match().

momesana
27th October 2007, 12:59
ugly implementation to give you a little bit of a hint on how to do it.



#include <QApplication>
#include <QtGui>

void listFiles(QDir, QString);

//#include "main.moc"
int main(int argc, char* argv[])
{
QApplication(argc, argv);
QDir dir("mydir");
listFiles(dir, "");
return 0;
}


void listFiles(QDir directory, QString indent)
{
indent += "\t";
QDir dir(directory);
QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
foreach(QFileInfo finfo, list) {
qDebug() << indent + finfo.fileName() << endl;
if (finfo.isDir()) {
listFiles(QDir(finfo.absoluteFilePath()), indent);
}
}
}

ahmed_pf
12th April 2014, 13:57
//Show All Files On Folder And Sub-folder With extinction "*.mov"

ui->listWidget_2->clear();

QStringList nameFilters;

nameFilters << "*.mov";



QDirIterator dirIterator(ui->TextPath->toPlainText(),nameFilters, QDir::Files, QDirIterator::Subdirectories);
while (dirIterator.hasNext()) {
ui->listWidget_2->addItem(dirIterator.next());
}