Results 1 to 13 of 13

Thread: remove directory empty or not empty

Hybrid View

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

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

    raphaelf (27th October 2006)

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

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

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

    raphaelf (27th October 2006)

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.