Results 1 to 7 of 7

Thread: Count files of a directory

  1. #1
    Join Date
    Oct 2007
    Location
    Romania
    Posts
    13
    Qt products
    Qt4
    Platforms
    Windows

    Question Count files of a directory

    I want to count the number of files from a directory and all its subdirectories recursively. My code is:

    Qt Code:
    1. int DocumentsManager::CountFiles(QString path)
    2. {
    3. int sum = 0;
    4. QDir dir(path);
    5. dir.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);
    6. QFileInfoList lst = dir.entryInfoList();
    7. for(int i = 0; i < lst.size(); i++)
    8. {
    9. sum = sum + CountFiles(lst.at(i).canonicalPath());
    10. }
    11. dir.setFilter(QDir::Files);
    12. return dir.entryInfoList().size() + sum;
    13. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that sometimes I got the following error:
    ASSERT: i>=0 && i < size()

    Do you have any idea why? Thank you.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Count files of a directory

    What happens if "path" points to a file? I don't know if you can construct a QDir from that. I think you should revise your function and not call it for files, only for sub dirs.

  3. #3
    Join Date
    Oct 2007
    Location
    Romania
    Posts
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Count files of a directory

    It does not contains files because the filter returns only directory excluding . and .. The Problem is that it crashes only if i add My Documents folder. And i cannot debug that error

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Count files of a directory

    Do you have permissions in that folder and all sub folders?

  5. #5
    Join Date
    Oct 2007
    Location
    Romania
    Posts
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Count files of a directory

    I didn't think of that .. but my user is admin ... so i should have permissions.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Count files of a directory

    I'd do:

    Qt Code:
    1. int countFiles(const QString &path, bool countDirs=false){
    2. QFileInfo finfo(path);
    3. if(!finfo.exists() || !finfo.isDir())
    4. return 0;
    5. int res = 0;
    6. QStringList slist = finfo.absoluteDir().entryList(QDir::Dirs|QDir::NoSymLinks|QDir::NoDotAndDotDot);
    7. foreach(QString path, slist){
    8. QFileInfo childinfo(path);
    9. if(childInfo.isDir()){
    10. if(countDirs)
    11. res++;
    12. res+=countFiles(path, countDirs);
    13. } else
    14. res++;
    15. }
    16. return res;
    17. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Mar 2012
    Posts
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Count files of a directory

    int uiPrincipal::CountFiles(QString path)
    {
    int suma = 0;
    QDir dir(path);
    dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
    if(!dir.exists()) {
    return 1;
    }
    QFileInfoList sList = dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);

    foreach(QFileInfo ruta, sList){
    if(ruta.isDir()){
    suma += CountFiles(ruta.path() + "/" + ruta.completeBaseName()+"/");
    }
    suma++;
    }
    return suma;
    }

Similar Threads

  1. trace new files in a directory
    By Fastman in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2007, 12:04
  2. Replies: 42
    Last Post: 30th July 2007, 14:55
  3. [QT4] Counting files in a directory (Linux)
    By ucntcme in forum Qt Programming
    Replies: 2
    Last Post: 24th July 2007, 22:59
  4. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  5. Am I the only one with "make" error ?
    By probine in forum Installation and Deployment
    Replies: 1
    Last Post: 13th February 2006, 12:54

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.