Hi,

I'm subclassing QDir for a "removeAll" method to fully remove a directory with all its sub-directories.

My problem is the error-handling. To avoid infinity loops, I break the iteration if QFile::remove() returns false.

When all files are closed, in some cases even if they are open, the removeAll method does great job.

BUT:
Let's take an example situation:
I have a bitmap test.bmp in a sub-directory.
Now there are 3 cases:
1. test.bmp is closed --> everything works fine and test.bmp is deleted
2. test.bmp is opend for viewing (doubleclick on file) --> everything works fine and test.bmp is deleted (although it is still opened on my screen, but when I close the window, it is deleted)
3. test.bmp is opend for editing (paint.exe) --> infinity loop, because it is not deleted and program always tries and fails to delete the parent directory!!

That is the code:
Qt Code:
  1. myQDir::myQDir(const QString &stringPath) : QDir(stringPath)
  2. {
  3. this->diriteratorAllEntries = new QDirIterator(stringPath, QDir::AllEntries | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
  4. this->diriteratorAllSubDirs = new QDirIterator(stringPath, QDir::AllDirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
  5. this->stringlistAllSubDirs = new QStringList;
  6. this->stringlistErrors = new QStringList;
  7. this->stringlistErrors->clear();
  8. }
  9.  
  10. bool myQDir::removeAll(bool boolIncludeParent)
  11. {
  12. this->stringlistErrors->clear();
  13. QString stringEntry;
  14. while(this->diriteratorAllEntries->hasNext())
  15. {
  16. stringEntry = this->diriteratorAllEntries->next();
  17. if(QDir(stringEntry).exists())
  18. {
  19. this->stringlistAllSubDirs->append(stringEntry);
  20. continue;
  21. }
  22. if(!QFile::remove(stringEntry)) // there is the problem --> QFile::remove() always returns true, even when the bitmap is not deleted!!
  23. this->stringlistErrors->append(stringEntry);
  24. }
  25. if(this->stringlistErrors->count() > 0) // this should break when any files are not deleted and prevent from going into the next loop where the directories are deleted --> also fails!!
  26. return false;
  27.  
  28. while(this->stringlistAllSubDirs->count() > 0)
  29. {
  30. for(int i = 0; i < this->stringlistAllSubDirs->count(); i++)
  31. {
  32. if(QDir(this->stringlistAllSubDirs->at(i)).rmdir(this->stringlistAllSubDirs->at(i)))
  33. this->stringlistAllSubDirs->removeAt(i);
  34. }
  35. }
  36. if(boolIncludeParent)
  37. {
  38. if(!this->rmdir(this->absolutePath()))
  39. return false;
  40. }
  41. return true;
  42. }
To copy to clipboard, switch view to plain text mode 

Does anybody know why QFile::remove fails when the bitmap is opend for editing? Maybe it doesn't realize that a second program (e.g. paint.exe) has access to it... Don't know.

Thank you in anticipation!
Binary