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
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

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

    Do it "urself"!


    Regards

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

    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

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

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

    Here it is. I just tested it:

    This is where it starts( the wrapper):
    Qt Code:
    1. void qttest::clearAll()
    2. {
    3. QString dirRoot = "c:\\testdir";
    4. clearAll(dirRoot);
    5. QDir d(dirRoot);
    6. d.cdUp();
    7. d.rmdir(dirRoot);
    8. }
    To copy to clipboard, switch view to plain text mode 


    This is the actual function:
    Qt Code:
    1. void qttest::clearAll(QString dirPath)
    2. {
    3. QDir dir(dirPath);
    4. QList<QFileInfo> infLst = dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);
    5. foreach(QFileInfo finfo, infLst )
    6. {
    7. if(finfo.isDir() )
    8. {
    9. clearAll(finfo.absoluteFilePath());
    10. dir.rmdir(finfo.absoluteFilePath());
    11. }
    12. else
    13. { //file
    14. dir.remove(finfo.absoluteFilePath());
    15. }
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 


    Regards.

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

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

    Hi Marcel


    Can u pls tell me in function

    Qt Code:
    1. void qttest::clearAll()
    2. {
    3. QString dirRoot = "c:\\testdir";
    4. clearAll(dirRoot);
    5. QDir d(dirRoot);
    6. d.cdUp();
    7. d.rmdir(dirRoot);
    8. }
    To copy to clipboard, switch view to plain text mode 


    Why you used this(4th line of the above code) that is:

    Qt Code:
    1. clearAll(dirRoot);
    To copy to clipboard, switch view to plain text mode 

    Thanx
    Always Believe in Urself
    Merry

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

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

    You should pass there the directory returned by the file dialog.
    clearAll() without any parameters is just an overloaded variant of the second, provided for convenince.

    It is really not that different than the first version.
    I just passed a folder that I created on my hard disk.

    And don't say it doesn't work because it does. If you don't mess up the code, then it will work for you too.

    Regards

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

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

    Hi Marcel

    Thanx Marcel for helping me so much

    Pls dont get angry on me....

    I think I am not messing the code now , It is same as the code u sent to me , Is'nt it...

    But it also deletes the files but not folders.....


    Qt Code:
    1. void Form1::ShowFileDialog()
    2. {
    3. m_file=QFileDialog::getExistingDirectory(this, tr("Open Directory"),
    4. "/home",
    5. QFileDialog::ShowDirsOnly
    6. | QFileDialog::DontResolveSymlinks);
    7.  
    8.  
    9.  
    10. filename->setText(m_file);
    11.  
    12. }
    13.  
    14. void Form1::DeleteData()
    15. {
    16. QString dirRoot = m_file;
    17. DeleteData(dirRoot);
    18. QDir d(dirRoot);
    19. d.cdUp();
    20. d.rmdir(dirRoot);
    21. }
    22. ////////////////////////////////////////////////////////////////////////
    23.  
    24. /*Function for erasing*/
    25.  
    26. void Form1::DeleteData(QString dirViewPath)
    27. {
    28. QDir dirPath(dirViewPath);
    29. if (dirPath.exists())
    30. {
    31. QList<QFileInfo> infLst = dirPath.entryInfoList();
    32. for(int i=0;i<infLst.size();i++)
    33. {
    34. QFileInfo strFileInfo = infLst.at(i);
    35. QString strFileName = strFileInfo.fileName();
    36. if(!(strFileInfo.fileName() == "." || strFileInfo.fileName() == ".."))
    37. {
    38. if(strFileInfo.isDir())
    39. {
    40. DeleteData(strFileInfo.absoluteFilePath());
    41. dirPath.rmdir(strFileInfo.absoluteFilePath());
    42. }
    43. else
    44. {
    45.  
    46. dirPath.remove(strFileInfo.absoluteFilePath());
    47.  
    48. }
    49. }
    50.  
    51. }
    52. }
    53. }
    To copy to clipboard, switch view to plain text mode 

    Thanx
    Always Believe in Urself
    Merry

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

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

    Are you sure you have permissions to delete the directories?
    Are you running as root or as a normal user?

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

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

    I m running as a normal user
    Always Believe in Urself
    Merry

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

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

    Well, could you try running it as root?

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

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

    I tried it running as a root

    It works , but for that directories only which contains files only.

    not for that directories which contains both files and folders ..


    Thanx
    Always Believe in Urself
    Merry

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

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

    Sorry, but can't help you. I really can't understand what's going on.
    I tested it on the following structure. Every directory in there had 1,2 or three files in it.
    So I can't say what is going on in your case.

    Regards
    Attached Images Attached Images

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

    merry (30th July 2007)

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

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

    One more thing:
    Make sure there isn't any browser opened on any of the directories you're trying to delete.
    Actually, close all the browsers(file system browsers) and run the program.

    Regards

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.