Page 1 of 3 123 LastLast
Results 1 to 20 of 43

Thread: How to erase all the files and folders in the Selected existing directory

  1. #1
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default How to erase all the files and folders in the Selected existing directory

    Hi all

    Working on Qt4.2

    Problem :

    I want to delete all the flles and folders contained in the directory that is selected by QFileDialog.

    Pls help , if anyboby know.......


    Thanx
    Always Believe in Urself
    Merry

  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: How to erase all the files and folders in the Selected existing directory

    Qt Code:
    1. QDir dir = ...//initialize with the return of the file dlg
    2. void recursiveDelete(const QDir &dir )
    3. {
    4. QList<QFileInfo> infLst = dir.entryInfoList();
    5. foreach(QFileInfo finfo, infList )
    6. {
    7. if(fInfo.isDir() )
    8. {
    9. QDir subDir(finfo.absolutePath());
    10. recursiveDelete(subDir);
    11. dir.rmdir(subDir);
    12. }
    13. else { //file
    14. dir.remove(finfo.absolutePath());
    15. }
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 
    This code is not tested so you may have to polish it a bit .

    But the idea is correct.

    Regards

  3. The following user says thank you to marcel for this useful post:

    punkypogo (2nd June 2008)

  4. #3
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to erase all the files and folders in the Selected existing directory

    You should specify QDir::NoDotAndDotDot. I.e.
    Qt Code:
    1. QList<QFileInfo> infLst = dir.entryInfoList(QDir::NoDotAndDotDot);
    To copy to clipboard, switch view to plain text mode 
    you should also be very careful with this function. Otherwise you'll get into trouble faster than you can say
    Qt Code:
    1. recursiveDelete(QDir("/"))
    To copy to clipboard, switch view to plain text mode 

  5. #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: How to erase all the files and folders in the Selected existing directory

    Quote Originally Posted by spud View Post
    You should specify QDir::NoDotAndDotDot. I.e.
    Qt Code:
    1. QList<QFileInfo> infLst = dir.entryInfoList(QDir::NoDotAndDotDot);
    To copy to clipboard, switch view to plain text mode 
    you should also be very careful with this function. Otherwise you'll get into trouble faster than you can say
    Qt Code:
    1. recursiveDelete(QDir("/"))
    To copy to clipboard, switch view to plain text mode 
    I already said this code is not tested.
    It is not meant just to be copy-pasted in an application.
    It is just a draft.

    So, whoever decides to use it should adapt it to their own needs.

    Regards

  6. The following user says thank you to marcel for this useful post:

    merry (26th July 2007)

  7. #5
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to erase all the files and folders in the Selected existing directory

    Thanx

    It works , It deletes all the files contained in the folder,

    but is there any other way , that after deleting all the files of the folder it also deletes that folder .


    Thanx
    Always Believe in Urself
    Merry

  8. #6
    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: How to erase all the files and folders in the Selected existing directory

    Yes, just call rmdir for the original QDir, that you pass to the funciton.
    You have to call this after the function returns.

    Regards

  9. #7
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to erase all the files and folders in the Selected existing directory

    Thanx

    I do like this only ,but it wont works...

    Pls view code...


    Qt Code:
    1. void Form1::DeleteData(QString dirViewPath)
    2. {
    3. QDir dirPath(dirViewPath);
    4. QList<QFileInfo> infLst = dirPath.entryInfoList();
    5. for(int i=0;i<infLst.size();i++)
    6. {
    7. QFileInfo strFileInfo = infLst.at(i);
    8. QString strFileName = strFileInfo.fileName();
    9. if(!(strFileInfo.fileName() == "." || strFileInfo.fileName() == ".."))
    10. {
    11. if(strFileInfo.isDir())
    12. {
    13. QString strFilePath = dirViewPath + "/" + strFileInfo.fileName();
    14. QString fileName = strFileInfo.fileName();
    15. DeleteData(strFilePath);
    16. dirPath.rmdir(dirViewPath);
    17. }
    18. else
    19. {
    20. QString fileName = dirViewPath + "/" + strFileInfo.fileName();
    21. QFile file1(fileName);
    22. file1.remove();
    23. }
    24. }
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 
    Always Believe in Urself
    Merry

  10. #8
    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: How to erase all the files and folders in the Selected existing directory

    DeleteData does not work?
    Or removing the parent dir?
    I assume you call it like this:
    Qt Code:
    1. ...
    2. QString dirViewPath = ...
    3. DeleteData(dirViewPath);
    4. //Also add the following two lines:
    5. QDir dir(dirViewPath);
    6. dir.rmdir();
    7. ...
    To copy to clipboard, switch view to plain text mode 

    Or is it a slot?

    Regards

  11. #9
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to erase all the files and folders in the Selected existing directory

    HI
    DeleteData does not work?
    Or removing the parent dir?
    I assume you call it like this:
    ...
    QString dirViewPath = ...
    DeleteData(dirViewPath);
    //Also add the following two lines:
    QDir dir(dirViewPath);
    dir.rmdir();
    Pls view the code I sent you in the last post,I think I had added the above two lines..

    thanx
    Always Believe in Urself
    Merry

  12. #10
    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: How to erase all the files and folders in the Selected existing directory

    Oh, yes, I see now.
    That will not work because at that point the parent dir is possible not to be empty, therefore the call will fail.

    Do it like I have shown you.

    Another solution is:
    Qt Code:
    1. void Form1::DeleteData(String dirViewPath)
    2. {
    3. ...
    4. //function body
    5. ...
    6.  
    7. if(!dirPath.count()==2) //"." and ".." -- might not be the case. if not,. test with 0
    8. {
    9. dirPath.cdUp();
    10. dirPath.rmdir(dirViewPath);
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    This will work. I forgot to mention that you have to go one level above dirViewPath, in order to delete it.

    Regards

  13. #11
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to erase all the files and folders in the Selected existing directory

    Thanx for the reply

    can u pls explain me why u had used this line

    Qt Code:
    1. if(!dirPath.count()==2)
    To copy to clipboard, switch view to plain text mode 

    and also I had used this also

    Qt Code:
    1. if(!dirPath.count()==2) //"." and ".." -- might not be the case. if not,. test with 0
    2. {
    3. dirPath.cdUp();
    4. dirPath.rmdir(dirViewPath);
    5. }
    To copy to clipboard, switch view to plain text mode 

    but it also not works....
    Always Believe in Urself
    Merry

  14. #12
    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: How to erase all the files and folders in the Selected existing directory

    I am at work and I write these in a hurry.
    Remove the "!".
    I added those because count might return 2 even if the dir is empty( 2 for . and .. ).

    Regards

  15. #13
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to erase all the files and folders in the Selected existing directory

    Thanx Marcel

    But it also not works
    Always Believe in Urself
    Merry

  16. #14
    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: How to erase all the files and folders in the Selected existing directory

    This means that "." and ".." are ignore by count().
    Compare with 0 instead of 2 and you'll get it done.

    Regards

  17. #15
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to erase all the files and folders in the Selected existing directory

    Sorry

    It again not works

    regards
    Always Believe in Urself
    Merry

  18. #16
    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: How to erase all the files and folders in the Selected existing directory

    Then just delete it outside the function after the directory's contents are all removed.
    I think you don't "believe in urself" .

    Regards

  19. #17
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to erase all the files and folders in the Selected existing directory

    Thank You very much for telling me that

    I dont beleive in myself
    Always Believe in Urself
    Merry

  20. #18
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to erase all the files and folders in the Selected existing directory

    hi

    Is there anybody Who tells me how to delete folders after the deletion of files..

    Actually all the files contained in the folders are deleted but only the blank folders are left

    how can i delete folders , pls help....

    I used this also but nothing happens

    Qt Code:
    1. if(dirPath.count()==0)
    2. {
    3. dirPath.cdUp();
    4. dirPath.rmdir(dirViewPath);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Thanx
    Always Believe in Urself
    Merry

  21. #19
    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: How to erase all the files and folders in the Selected existing directory

    Do it "urself"!


    Regards

  22. #20
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to erase all the files and folders in the Selected existing directory

    Thanx Marcel for the reply

    Ok I 'll do it myself only.
    Always Believe in Urself
    Merry

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.