Results 1 to 20 of 43

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  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

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

    Regards

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

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

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

    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

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

    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

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

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

    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

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

    Thanx Marcel

    But it also not works
    Always Believe in Urself
    Merry

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

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

    Regards

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

    Sorry

    It again not works

    regards
    Always Believe in Urself
    Merry

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

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

    Regards

  13. #13
    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 merry View Post
    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
    But you said that the contained files and folders are deleted.
    I understood that your only problem is the parent folder. You couldn't delete that.

    Which one is it?

    Regards

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

    Actually I said that it deletes all the Files that are in the folders , and empty folders are left.....,

    eg..

    If there is a folder name "dir1" it further contains files(F1 , F2) and folder dir2.

    then F1 and F2 are deleted but empty dir1 and dir2 folders are left....

    My question is how can i delete empty folders?

    Thanx
    Always Believe in Urself
    Merry

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

    But you messed up the code.
    In the case you run into a directory:
    Qt Code:
    1. QDir subDir(finfo.absolutePath());
    2. dir.rmdir(subDir);
    To copy to clipboard, switch view to plain text mode 

    You removed that.
    It should work, as long as the directory is not used/empty.
    Couldn't you debug it to see exactly what is wrong?

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

    Qt Code:
    1. QDir subDir(finfo.absolutePath());
    2. dir.rmdir(subDir);
    To copy to clipboard, switch view to plain text mode 

    Are u sure it works..
    The function is like this only...

    QDir::rmdir ( const QString & dirName )

    and subDir is of QDir type...

    and parameters passed in the function are of QString type...


    Thanx
    Always Believe in Urself
    Merry

  17. #17
    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 merry View Post
    Qt Code:
    1. QDir subDir(finfo.absolutePath());
    2. dir.rmdir(subDir);
    To copy to clipboard, switch view to plain text mode 
    Are u sure it works..
    The function is like this only...

    QDir::rmdir ( const QString & dirName )

    and subDir is of QDir type...

    and parameters passed in the function are of QString type...

    Thanx
    Well, fix it!
    Pass finfo.absolutePath() to rmdir.
    subdir has to be empty at that moment. so,a rmdir from the parent should work.

    What about the debugging?

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.