PDA

View Full Version : Removing a directory entirely



Barry79
11th May 2009, 16:09
Hi,

This might help someone, tested on Ubuntu 9.04 -



bool LocalCaseList::removeDirectory(QDir &directory)
{
bool isSuccessful = true;

if (directory.exists())
{
QFileInfoList entries = directory.entryInfoList(QDir::NoDotAndDotDot | QDir::Hidden | QDir::Dirs | QDir::Files);
int count = entries.size();

for (int i = 0; ((i < count) && (isSuccessful == true)); i++)
{
QFileInfo entryInfo = entries[i];
QString path = entryInfo.absoluteFilePath();
if (entryInfo.isDir())
{
QDir innerDirectory = QDir(path);
isSuccessful = removeDirectory(innerDirectory);
}
else
{
QFile file(path);
if (!file.remove())
isSuccessful = false;
}
}
if (!directory.rmdir(directory.absolutePath()))
isSuccessful = false;
}

return(isSuccessful);
}