Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 43

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

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

    Quote Originally Posted by marcel View Post
    Then just delete it outside the function after the directory's contents are all removed.

    Regards
    Have you tried this?
    Can you post a bigger part of the code?
    But first try deleting the dir after you call the recursive function.

    Regards

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

    Really I tried it lots & lots of times but it does not remove directories,It removes all the files but not directories .

    Ok Pls view my code , and tell me is I m doin something wrong.

    Qt Code:
    1. void Form1::DeleteData(QString dirViewPath)
    2. {
    3. QDir dirPath(dirViewPath);
    4. if (dirPath.exists())
    5. {
    6. QList<QFileInfo> infLst = dirPath.entryInfoList();
    7. for(int i=0;i<infLst.size();i++)
    8. {
    9. QFileInfo strFileInfo = infLst.at(i);
    10. QString strFileName = strFileInfo.fileName();
    11. if(!(strFileInfo.fileName() == "." || strFileInfo.fileName() == ".."))
    12. {
    13. if(strFileInfo.isDir())
    14. {
    15. QString strFilePath = dirViewPath + "/" + strFileInfo.fileName();
    16. QString fileName = strFileInfo.fileName();
    17. DeleteData(strFilePath);
    18. }
    19. else
    20. {
    21. unsigned char *pBuff;
    22. FILE *fh = NULL;
    23. QString fileName = dirViewPath + "/" + strFileInfo.fileName();
    24. QFile file1(fileName);
    25. long SizeToRead =fileName.size();
    26. fh=fopen((const char*)fileName.toAscii() ,"wb+");
    27. pBuff = new unsigned char[SizeToRead];
    28. QString str;
    29. str = comboBox->itemText(comboBox->currentIndex());
    30. bool ok;
    31. int val= str.toInt(&ok,36);
    32. memset(pBuff,val,SizeToRead);
    33. fwrite((void*)pBuff,SizeToRead,1,fh);
    34. delete []pBuff;
    35. pBuff=NULL;
    36. fclose(fh);
    37. file1.remove();
    38. }
    39. }
    40. }
    41. dirPath.cdUp();
    42. dirPath.rmdir(dirViewPath);
    43. }
    44. }
    To copy to clipboard, switch view to plain text mode 

    Thanx
    Always Believe in Urself
    Merry

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

    But I really wanted to know where you call DeleteData.
    Could you post that part of the code?

    Regards

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

    I m sending u both .h and .cpp files Pls view this code.

    Form1.h
    Qt Code:
    1. class Form1:public QDialog,private Ui::Form1
    2. {
    3. Q_OBJECT
    4. public:
    5. Form1::Form1(QWidget *parent): QDialog(parent)
    6. {
    7. setupUi(this);
    8. connect(File,SIGNAL(clicked()),this,SLOT(ShowFileDialog()));
    9. connect(Erase,SIGNAL(clicked()),this,SLOT(CallDeleteData()));
    10. };
    11. private slots:
    12. void ShowFileDialog();
    13. void DeleteData(QString);
    14. void CallDeleteData();
    15. protected:
    16. QString m_file;
    17. };
    To copy to clipboard, switch view to plain text mode 

    Form1.cpp


    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. filename->setText(m_file);
    9.  
    10. }
    11.  
    12. void Form1::CallDeleteData()
    13. {
    14. DeleteData(m_file);
    15. }
    16. ////////////////////////////////////////////////////////////////////////
    17.  
    18. /*Function for erasing*/
    19.  
    20. void Form1::DeleteData(QString dirViewPath)
    21. {
    22. QDir dirPath(dirViewPath);
    23. if (dirPath.exists())
    24. {
    25. QList<QFileInfo> infLst = dirPath.entryInfoList();
    26. for(int i=0;i<infLst.size();i++)
    27. {
    28. QFileInfo strFileInfo = infLst.at(i);
    29. QString strFileName = strFileInfo.fileName();
    30. if(!(strFileInfo.fileName() == "." || strFileInfo.fileName() == ".."))
    31. {
    32. if(strFileInfo.isDir())
    33. {
    34. QString strFilePath = dirViewPath + "/" + strFileInfo.fileName();
    35. QString fileName = strFileInfo.fileName();
    36. DeleteData(strFilePath); //recursive Calling
    37. }
    38. else
    39. {
    40. unsigned char *pBuff;
    41. FILE *fh = NULL;
    42. QString fileName = dirViewPath + "/" + strFileInfo.fileName();
    43. QFile file1(fileName);
    44. long SizeToRead =fileName.size();
    45. fh=fopen((const char*)fileName.toAscii() ,"wb+");
    46. pBuff = new unsigned char[SizeToRead];
    47. QString str;
    48. str = comboBox->itemText(comboBox->currentIndex());
    49. bool ok;
    50. int val= str.toInt(&ok,36);
    51. memset(pBuff,val,SizeToRead);
    52. fwrite((void*)pBuff,SizeToRead,1,fh);
    53. delete []pBuff;
    54. pBuff=NULL;
    55. fclose(fh);
    56. file1.remove();
    57. }
    58. }
    59. }
    60. dirPath.cdUp();
    61. dirPath.rmdir(dirViewPath);
    62. }
    63. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by merry; 30th July 2007 at 09:56. Reason: updated contents
    Always Believe in Urself
    Merry

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

    In CallDeleteData():
    Qt Code:
    1. void Form1::CallDeleteData()
    2. {
    3. DeleteData(m_file);
    4. QDir dir(m_file);
    5. if( dir.cdUp() )
    6. {
    7. dir.rmdir(m_file);
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    Also, remove lines 60 and 61 from DeleteData. They are not needed anymore.

    Regards

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

    I tried it , but directories are still there..

    DeleteData function is recursively Calling.....

    Thanx...
    Always Believe in Urself
    Merry

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

    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

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

    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

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

    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?

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

    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

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

    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?

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

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

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

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

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

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

  18. #38
    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?

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

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

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

    merry (30th July 2007)

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.