Results 1 to 6 of 6

Thread: how to list out all files in a dir and sub dir

  1. #1
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Question how to list out all files in a dir and sub dir

    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 ?

    Qt Code:
    1. QDir dir(wsPath);
    2. dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
    3. QFileInfoList list = dir.entryInfoList();
    4. for (int i = 0; i < list.size(); ++i)
    5. {
    6. if(list.at(i).completeSuffix() == "txt" )
    7. {
    8.  
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    Regards
    Rajesh
    Last edited by jacek; 14th August 2006 at 11:32. Reason: changed [ html ] tags to [ code ] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to list out all files in a dir and sub dir

    How about:
    Qt Code:
    1. QDir dir(wsPath);
    2. foreach( const QFileInfo& entry, dir.entryInfoList( QStringList() << "*.txt", QDir::Files | QDir::Hidden | QDir::NoSymLinks ) ) {
    3. //...
    4. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Unhappy Re: how to list out all files in a dir and sub dir

    Thanks Jacek,


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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to list out all files in a dir and sub dir

    Qt Code:
    1. QString nameFilter( "*.txt" );
    2. QDir::Filter filter = QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::NoSymLinks;
    3.  
    4. push dir
    5. while not empty:
    6. pop dir
    7. foreach( const QFileInfo& entry, dir.entryInfoList( nameFilter, filter ) ) {
    8. if( entry.isDir() ) {
    9. push entry.dir();
    10. }
    11. else {
    12. // ...
    13. }
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to jacek for this useful post:

    rajesh (14th August 2006)

  6. #5
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: how to list out all files in a dir and sub dir

    Jacek,
    push pop gives complier error

  7. #6
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: how to list out all files in a dir and sub dir

    Thanks Jacek,
    I used QList and done push and pop, so its working now.
    Thanks again.

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.