PDA

View Full Version : how to list out all files in a dir and sub dir



rajesh
14th August 2006, 12:15
Hi!
I have written following code to find all files in given directories, but the following code does not give the list of all files available in sub directory too.
How to get all file names?
And how to filter only specific extension files eg: only .txt file ?



QDir dir(wsPath);
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
QFileInfoList list = dir.entryInfoList();
for (int i = 0; i < list.size(); ++i)
{
if(list.at(i).completeSuffix() == "txt" )
{

}
}


Regards
Rajesh

jacek
14th August 2006, 12:36
How about:
QDir dir(wsPath);
foreach( const QFileInfo& entry, dir.entryInfoList( QStringList() << "*.txt", QDir::Files | QDir::Hidden | QDir::NoSymLinks ) ) {
//...
}

rajesh
14th August 2006, 12:51
Thanks Jacek,


Your code solved only 2nd requirement. But my basic requirement is how to get subdirectories file?

jacek
14th August 2006, 13:22
QString nameFilter( "*.txt" );
QDir::Filter filter = QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::NoSymLinks;

push dir
while not empty:
pop dir
foreach( const QFileInfo& entry, dir.entryInfoList( nameFilter, filter ) ) {
if( entry.isDir() ) {
push entry.dir();
}
else {
// ...
}
}
}

rajesh
14th August 2006, 13:37
Jacek,
push pop gives complier error

rajesh
14th August 2006, 15:57
Thanks Jacek,
I used QList and done push and pop, so its working now.
Thanks again.