PDA

View Full Version : Reading files from a perticular folder



suneel1310
10th March 2009, 07:24
Hi all.

I have an application which takes a file name as the input.
Currently I am hard coding the file name in the constructor.

But, I want to read all the files from a perticular folder one by one.
How to do that ??
Using QFileDialog class i can only select a perticular file (or files) from a list of file :(
Thank you.

spirit
10th March 2009, 07:29
so, what is the problem? store files names in a list and then implement a loop in which you can read data from each file. :)

LordQt
10th March 2009, 11:02
Hello my friend,

look at this little snippet from me perhaps it will help you.



QString somepath = "C:/yourpathtofolder/"
QDir somefolder = QDir(somepath);
somefolder.setFilter(QDir::Files | QDir::Dirs | QDir::NoSymLinks);
somefolder.setSorting(QDir::Size | QDir::Reversed);


QStringList filenames = somefolder.entryList(QDir::NoDotAndDotDot | QDir::AllDirs, QDir::Name);
QStringList fileListing;

for (int i(0); i < filenames.count(); i++)
{
QString str = filenames.at(i);
//you can also ignore the if statement for filtering your Filenames depends on your filtertext
QDir dir(somepath+filenames.at(i));
foreach ( QString file, dir.entryList( QDir::Files))
{
if(file.contains("YourFilterText", Qt::CaseInsensitive))
fileListing << QFileInfo( dir, file).absoluteFilePath() << "\n";
}
}
//after you fill up your list and do want you want to do my friend ;o))
foreach (QString str, fileListing)
{
QMessageBox::information(this, tr("My Files in Folder"),
tr("File: %1 "
" ")
.arg(str));
}
fileListing.clear();


I hope I don´t make any mistake, please proof it before using ;o))

bye