Results 1 to 13 of 13

Thread: remove directory empty or not empty

  1. #1
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default remove directory empty or not empty

    Hi everybody,

    I have a folder "artistx". This folder could be empty or not.
    I would like to delete folder "artistx" and if it contains files or folder or not, i would like to delete it.
    I tried like this: (I can just remove the folder if the folder is empty)

    Qt Code:
    1. QString artistDirectory(path + "/" + artist);
    2. QMessageBox::information(this,"",artistDirectory);
    3. QDir directoryTodelete;
    4.  
    5.  
    6. directoryTodelete.rmpath(artistDirectory);
    To copy to clipboard, switch view to plain text mode 
    Think DigitalGasoline

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: remove directory empty or not empty

    You will need to write a recursive function to do this.

    Something like this

    Qt Code:
    1. void remove(const QString &path)
    2. {
    3. QFileInfo fileInfo(path);
    4. if(fileInfo.isDir()){
    5. QDir dir(path);
    6. QStringList fileList = dir.entryList();
    7. for(int i = 0; i < fileList.count(); ++i){
    8. remove(fileList.at(i));
    9. }
    10. rmpath(path);
    11. }
    12. else{
    13. QFile::remove(path);
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

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

    raphaelf (27th October 2006)

  4. #3
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: remove directory empty or not empty

    Hi,
    I have a folder under c:\temp with the name aa1.
    This folder contains a folder qq1. And qq1 contains a file.
    I was not able to delete aa1, qq1 and the file.
    What is wrong here:

    Qt Code:
    1. QString pathTest = "C:/temp/aa1";
    2. QFileInfo fileInfo(path);
    3. if(fileInfo.isDir())
    4. {
    5. QDir dir(pathTest);
    6. QMessageBox::information(this,"pathTest",pathTest);
    7. QStringList fileList = dir.entryList();
    8.  
    9. for(int i = 0; i < fileList.count(); ++i)
    10. {
    11. QMessageBox::information(this,"fileList.at(i)",fileList.at(i));
    12. remove(fileList.at(i));
    13. }
    14. rmpath(pathTest);
    15. }
    16. else
    17. {
    18. QFile::remove(pathTest);
    19. }
    To copy to clipboard, switch view to plain text mode 
    Think DigitalGasoline

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: remove directory empty or not empty

    You are not calling QDir::rmdir() anywhere, you only delete files.

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

    raphaelf (27th October 2006)

  7. #5
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: remove directory empty or not empty

    try replacing

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

    with

    Qt Code:
    1. dir.rmdir(path);
    To copy to clipboard, switch view to plain text mode 

    Also, I see that the function is not recursive.

  8. The following user says thank you to munna for this useful post:

    raphaelf (27th October 2006)

  9. #6
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: remove directory empty or not empty

    Hi

    Why can i not delete artist1 under c:\temp??The folder artist1 contains a folder album1 and album1 contains a file a.jpg.

    What is wrong?

    Qt Code:
    1. QString pathTest = "C:/temp/artist1";
    2. QFileInfo fileInfo(pathTest);
    3. if(fileInfo.isDir())
    4. {
    5. QDir dir(pathTest);
    6. QMessageBox::information(this,"pathTest",pathTest);
    7. QStringList fileList = dir.entryList();
    8.  
    9. for(int i = 0; i < fileList.count(); ++i)
    10. {
    11. QMessageBox::information(this,"fileList.at(i)",fileList.at(i));
    12. remove(fileList.at(i));
    13. }
    14. dir.rmdir(pathTest);
    15. }
    16. else
    17. {
    18. QFile::remove(pathTest);
    19. }
    To copy to clipboard, switch view to plain text mode 
    Think DigitalGasoline

  10. #7
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: remove directory empty or not empty

    Something like this should work.

    Qt Code:
    1. void someFunction()
    2. {
    3. QString pathTest = "C:/temp/artist1";
    4. remove(pathTest);
    5. }
    6.  
    7. void remove(const QString &path)
    8. {
    9. QFileInfo fileInfo(path);
    10. if(fileInfo.isDir()){
    11. QDir dir(path);
    12. QStringList fileList = dir.entryList();
    13. for(int i = 0; i < fileList.count(); ++i){
    14. remove(fileList.at(i));
    15. }
    16. dir.rmdir(path);
    17. }
    18. else{
    19. QFile::remove(path);
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    I thought the code was pretty much self-explanatory

  11. The following user says thank you to munna for this useful post:

    raphaelf (27th October 2006)

  12. #8
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: remove directory empty or not empty

    Hi munna

    Have you test it?I am not able to delete any folder..

    Are you shure this should work?Its not working:

    Qt Code:
    1. void Manage::deleteArtist()
    2. {
    3. .
    4. .
    5. QString artist = ui.delete_artist_cb->currentText();
    6. QString path;
    7. QSqlQuery select_sound_path("select value from config_tbl where config ='dsm_path'");
    8. while(select_sound_path.next())
    9. {
    10. path = select_sound_path.value(0).toString();
    11. }
    12. QString artistDirectory(path + "/" + artist);
    13. removeArtistDirectory(artistDirectory);
    14. }
    15.  
    16. void Manage::removeArtistDirectory(const QString &artistDirectory)
    17. {
    18. QMessageBox::information(this,"","remove function");
    19. QMessageBox::information(this,"",artistDirectory);
    20. QFileInfo fileInfo(artistDirectory);
    21. if(fileInfo.isDir())
    22. {
    23. QDir dir(artistDirectory);
    24. QStringList fileList = dir.entryList();
    25. for(int i = 0; i < fileList.count(); ++i)
    26. {
    27. QMessageBox::information(this,"",fileList.at(i));
    28. remove(fileList.at(i));
    29. }
    30. dir.rmdir(artistDirectory);
    31. }
    32. else
    33. {
    34. QFile::remove(artistDirectory);
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 
    Think DigitalGasoline

  13. #9
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: remove directory empty or not empty

    Your removeArtistDirectory function is not recursive

    Inside the for loop, replace

    Qt Code:
    1. remove(fileList.at(i));
    To copy to clipboard, switch view to plain text mode 

    with

    Qt Code:
    1. removeArtistDirectory(fileList.at(i));
    To copy to clipboard, switch view to plain text mode 

    By the way, is this code not giving you any error?

  14. The following user says thank you to munna for this useful post:

    raphaelf (27th October 2006)

  15. #10
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: remove directory empty or not empty

    Hi

    No i didnt get a error message from MINGW

    With following code the for loop never end.
    Why??

    Qt Code:
    1. void Manage::removeArtistDirectory(const QString &artistDirectory)
    2. {
    3.  
    4. QFileInfo fileInfo(artistDirectory);
    5. if(fileInfo.isDir())
    6. {
    7. QDir dir(artistDirectory);
    8. QStringList fileList = dir.entryList();
    9. for(int i = 0; i < fileList.count(); ++i)
    10. {
    11. QMessageBox::information(this,"",fileList.at(i));
    12. removeArtistDirectory(fileList.at(i));
    13. }
    14. dir.rmdir(artistDirectory);
    15. }
    16. else
    17. {
    18. QFile::remove(artistDirectory);
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 
    Think DigitalGasoline

  16. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: remove directory empty or not empty

    Try excluding "."'s and ".."'s:
    Qt Code:
    1. QStringList fileList = dir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  17. The following user says thank you to jpn for this useful post:

    raphaelf (27th October 2006)

  18. #12
    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: remove directory empty or not empty

    Quote Originally Posted by raphaelf View Post
    Hi everybody,

    I have a folder "artistx". This folder could be empty or not.
    I would like to delete folder "artistx" and if it contains files or folder or not, i would like to delete it.
    I tried like this: (I can just remove the folder if the folder is empty)

    Qt Code:
    1. QString artistDirectory(path + "/" + artist);
    2. QMessageBox::information(this,"",artistDirectory);
    3. QDir directoryTodelete;
    4.  
    5.  
    6. directoryTodelete.rmpath(artistDirectory);
    To copy to clipboard, switch view to plain text mode 

    Remove line SqlLog & insert the full path to dir be remove....

    DownDir_RM("c:\\") remove all file!!
    Bee sure evry time the fulla path insert!!





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

  19. The following user says thank you to patrik08 for this useful post:

    raphaelf (27th October 2006)

  20. #13
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: remove directory empty or not empty


    Thank you very much!!

    Thanks to all

    Have a nice Day
    Think DigitalGasoline

Similar Threads

  1. Am I the only one with "make" error ?
    By probine in forum Installation and Deployment
    Replies: 1
    Last Post: 13th February 2006, 12:54
  2. QSettings again ... how to remove array elements
    By Mike in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 08:58

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.