Results 1 to 6 of 6

Thread: how to recursive a directory in qt?

  1. #1
    Join Date
    Mar 2006
    Posts
    8
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Default how to recursive a directory in qt?

    hi all:
    how to recursive a directory in qt?
    i use QFileInfoListIterator, but it failed.

    can anyone help me? thanks!

  2. #2
    Join Date
    Nov 2006
    Posts
    5
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to recursive a directory in qt?

    Something like this ?:

    Qt Code:
    1. void RecurseDirectory(const QString& sDir)
    2. {
    3. QDir dir(sDir);
    4. QFileInfoList list = dir.entryInfoList();
    5. for (int iList=0;iList<list.count();iList++)
    6. {
    7. QFileInfo info = list[iList];
    8.  
    9. QString sFilePath = info.filePath();
    10. if (info.isDir())
    11. {
    12. // recursive
    13. if (info.fileName()!=".." && info.fileName()!=".")
    14. {
    15. RecurseDirectory(sFilePath);
    16. }
    17. }
    18. else
    19. {
    20. // Do something with the file here
    21. }
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

  3. The following 2 users say thank you to Elgerton for this useful post:

    rivimey (23rd March 2013), WinchellChung (26th November 2007)

  4. #3
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to recursive a directory in qt?

    Quote Originally Posted by deweyjew View Post
    hi all:
    how to recursive a directory in qt?
    i use QFileInfoListIterator, but it failed.

    can anyone help me? thanks!

    Here You can recursive remove file & dir....

    Qt Code:
    1. void DownDir_RM(const QString d)
    2. {
    3. QDir dir(d);
    4. ///////SqlLog("order to delete dir:"+d+" ");
    5. if (dir.exists())
    6. {
    7. const QFileInfoList list = dir.entryInfoList();
    8. for (int l = 0; l < list.size(); l++)
    9. {
    10. fi = list.at(l);
    11. if (fi.isDir() && fi.fileName() != "." && fi.fileName() != "..")
    12. DownDir_RM(fi.absoluteFilePath());
    13. else if (fi.isFile())
    14. {
    15. QFile f( fi.absoluteFilePath() );
    16. bool ret = f.remove();
    17. if (!ret)
    18. /////////SqlLog("Can't remove: " + fi.absoluteFilePath() + " (write-protect?)");
    19. }
    20.  
    21. }
    22. //////////SqlLog("Remove: " + d + " ");
    23. dir.rmdir(d);
    24.  
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Mar 2006
    Posts
    8
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Post Re: how to recursive a directory in qt?

    i use qt/e2.3.10, my code is below, but it doesn't work, why?

    Qt Code:
    1. int addFileFromDir(const QString& d)
    2. {
    3. QDir dir( d );
    4. dir.setMatchAllDirs( true );
    5. dir.setSorting(QDir::DirsFirst | QDir::IgnoreCase);
    6. if( !dir.exists() )
    7. {
    8. return -1;
    9. }
    10. QString file;
    11. const QFileInfoList * list = dir.entryInfoList();
    12. if( list )
    13. {
    14. QFileInfoListIterator it(*list);
    15. QFileInfo *fi;
    16. while ( (fi = it.current()) != 0 )
    17. {
    18. qDebug( fi->absFilePath() );
    19. if ( !fi->isDir() )
    20. {
    21. //do something
    22. }
    23. else if ( fi->isDir() && (fi->fileName()!=".") && (fi->fileName()!="..") )
    24. {
    25. addFileFromDir( fi->absFilePath() );
    26. }
    27. ++it;
    28. }
    29. }
    30. return 0;
    31. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 7th November 2006 at 01:50. Reason: missing [code] tags

  6. #5
    Join Date
    Mar 2006
    Posts
    8
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to recursive a directory in qt?

    i know why, there is another bug in other code
    thank you all the same!

  7. #6
    Join Date
    Nov 2006
    Posts
    5
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to recursive a directory in qt?

    Quote Originally Posted by patrik08 View Post
    Qt Code:
    1. void DownDir_RM(const QString d)
    2. {
    3. ....
    4. }
    To copy to clipboard, switch view to plain text mode 
    And what is the difference between your and my example?

Similar Threads

  1. QDirModel - show directory ".."?
    By jamd in forum Qt Programming
    Replies: 3
    Last Post: 23rd April 2009, 19:51
  2. Difference a file and a directory
    By xgoan in forum Qt Programming
    Replies: 3
    Last Post: 11th August 2006, 12:02
  3. create file in another directory
    By raphaelf in forum Qt Programming
    Replies: 3
    Last Post: 16th February 2006, 10:04
  4. Am I the only one with "make" error ?
    By probine in forum Installation and Deployment
    Replies: 1
    Last Post: 13th February 2006, 12:54
  5. QT Service and working directory
    By Lele in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2006, 11:46

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
  •  
Qt is a trademark of The Qt Company.