Results 1 to 8 of 8

Thread: Return number of directories in a directories

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    628
    Thanks
    27
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Return number of directories in a directories

    Hi to all I have this code:

    Qt Code:
    1. QDir dir = QDir::root();
    2. if( !dir.cd("IrisDatabase") )
    3. {
    4. qWarning("Cannot find the directory, creating it...");
    5. dir.mkdir("IrisDatabase");
    6. qDebug("Created");
    7. }
    8. else
    9. qDebug("Directory ok");
    10.  
    11. uint num = dir.entryList().count();
    12. qDebug() << num;
    To copy to clipboard, switch view to plain text mode 

    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?
    Franco Amato

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Return number of directories in a directories

    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...

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Return number of directories in a directories

    Three clues:
    1) Above post
    2) Filters
    3) QDir::NoDotAndDotDot

  4. #4
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    628
    Thanks
    27
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Return number of directories in a directories

    Thank you very much to all
    Franco Amato

  5. #5
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    628
    Thanks
    27
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Return number of directories in a directories

    Lykurg I'm not and Linux.
    I'm under windows
    Franco Amato

  6. #6
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    628
    Thanks
    27
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Return number of directories in a directories

    Quote Originally Posted by Lykurg View Post
    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:

    Qt Code:
    1. QString path = dir.path(); // returns C:/IrisDatabase/1
    2. dir.cdUp();
    3. uint users = dir.count(); // returns 0 instead of 1
    To copy to clipboard, switch view to plain text mode 

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

  7. #7
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Return number of directories in a directories

    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?

  8. #8
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    628
    Thanks
    27
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Return number of directories in a directories [SOLVED]]

    I solved it thank you
    Last edited by franco.amato; 30th September 2010 at 00:36.
    Franco Amato

Similar Threads

  1. QFileDialog select multiple directories
    By bepaald in forum Newbie
    Replies: 4
    Last Post: 15th May 2012, 06:17
  2. Adding directories to resources
    By roxton in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2008, 13:00
  3. qtopiamake bug when same name file under different directories
    By izico in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 25th October 2007, 20:02
  4. Additional Library Directories Not Searched
    By TheGrimace in forum General Programming
    Replies: 1
    Last Post: 11th September 2007, 16:50
  5. How to create directories??
    By paranoid_android in forum Qt Programming
    Replies: 3
    Last Post: 9th March 2006, 17:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.