PDA

View Full Version : find all files with a certain extention + folder control



tommy
19th March 2009, 15:29
Hi,

I have 2 questions:

1) I need to find all files in the current folder that have a *.txt extention. Then I want to sequentially open these files and use them.
What is the easiest files to get all filenames with a certain extention?
I would like to obtain these files sequentially - find one, do something, find another, do something etc.

2) How do you detect the presence or absence of folders and how do you create new folders with Qt?

Thanks a lot!

arturo182
19th March 2009, 17:15
Here's an example of how to list files in a directory from Qt Documentation (link (http://doc.trolltech.com/4.5/qdir.html)):

#include <QDir>
#include <iostream>

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QDir dir;
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
dir.setSorting(QDir::Size | QDir::Reversed);

QFileInfoList list = dir.entryInfoList();
std::cout << " Bytes Filename" << std::endl;
for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
std::cout << qPrintable(QString("%1 %2").arg(fileInfo.size(), 10)
.arg(fileInfo.fileName()));
std::cout << std::endl;
}
return 0;
}

Directories can be created with mkdir (http://doc.trolltech.com/4.5/qdir.html#mkdir) and to check if they exist use exists (http://doc.trolltech.com/4.5/qdir.html#exists).