PDA

View Full Version : Return number of directories in a directories



franco.amato
29th September 2010, 18:13
Hi to all I have this code:


QDir dir = QDir::root();
if( !dir.cd("IrisDatabase") )
{
qWarning("Cannot find the directory, creating it...");
dir.mkdir("IrisDatabase");
qDebug("Created");
}
else
qDebug("Directory ok");

uint num = dir.entryList().count();
qDebug() << num;

I would know why dir.entryList().count() returns 2 when the "IrisDatabase" directory is empty instead of 0.
I would list the number of directories contained on IrisDatabase
Where my code is wrong?

Lykurg
29th September 2010, 18:24
Nothing is wrong with your code, but instead of asking you just should have a look on what is returned. I bet it is "." and ".." since you are on linux. And they are in you directory even if you just have created it.


Moving to newbie. At least this thread...

squidge
29th September 2010, 19:50
Three clues:
1) Above post
2) Filters
3) QDir::NoDotAndDotDot

franco.amato
29th September 2010, 19:55
Thank you very much to all

franco.amato
29th September 2010, 20:01
Lykurg I'm not and Linux.
I'm under windows

franco.amato
29th September 2010, 23:05
Nothing is wrong with your code, but instead of asking you just should have a look on what is returned. I bet it is "." and ".." since you are on linux. And they are in you directory even if you just have created it.


Moving to newbie. At least this thread...

Now I created a directory inside 'C:/IrisDatabase' directory named '1' so now I have this directory structure: 'C:/IrisDatabase/1'.
So I would know why this code:



QString path = dir.path(); // returns C:/IrisDatabase/1
dir.cdUp();
uint users = dir.count(); // returns 0 instead of 1

returns 0 instead of 1 ( in C:/IrisDatabase there is an empty directory named 1 ) and the dir.count() should returns 1

SixDegrees
29th September 2010, 23:21
Has it occured to you to try printing out the contents of QDir.entryList() to see exactly what's being counted?

Please read the documentation of the code you're trying to use before using it. Pay particular attention, in this case, to the fact that you can set filters on QDir that affect what types of files are reported.

You do realize that QDir.count() doesn't return the number of directories alone, right?

franco.amato
29th September 2010, 23:29
I solved it thank you