Пример #1
0
bool Util::RemoveDirectoryRecursively(const QString& dirPathName, bool removeParentDir)
{
    // http://john.nachtimwald.com/2010/06/08/qt-remove-directory-and-its-contents/

    bool success = true;
    QDir dir(dirPathName);
    if (!dir.exists())
        return true; //We wanted it deleted; it's already deleted, success!

    QFileInfoList entriesInfo = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System |
                                                  QDir::Hidden | QDir::AllDirs | QDir::Files);

    foreach (const QFileInfo& ei, entriesInfo)
    {
        if (ei.isDir())
            success = RemoveDirectoryRecursively(ei.absoluteFilePath());
        else
            success = QFile::remove(ei.absoluteFilePath());

        if (!success)
            return success;
    }

    if (removeParentDir)
        success = dir.rmdir(dirPathName);
    return success;
}
Пример #2
0
bool RemoveDirectoryRecursively(std::string path)
{
	//LogMsg(" RemoveDirectoryRecursively: %s", path.c_str());
	
	dirent * buf, * ent;
	DIR *dp;

	dp = opendir(path.c_str());
	if (!dp)
	{
		LogError("RemoveDirectoryRecursively: opendir failed");
		return false;
	}

	buf = (dirent*) malloc(sizeof(dirent)+512);
	while (readdir_r(dp, buf, &ent) == 0 && ent)
	{
		
		if (ent->d_name[0] == '.' && ent->d_name[1] == 0) continue;
		if (ent->d_name[0] == '.' && ent->d_name[1] == '.' && ent->d_name[2] == 0) continue;

		//LogMsg("Got %s. type %d", ent->d_name, int(ent->d_type));
		if (ent->d_type == DT_REG) //regular file
		{
			std::string fName = path+std::string("/")+ent->d_name;
			//LogMsg("Deleting %s", fName.c_str());
			unlink( fName.c_str());
		}

		if (ent->d_type == DT_DIR) //regular file
		{
			std::string fName = path+std::string("/")+ent->d_name;
			//LogMsg("Entering DIR %s",fName.c_str());
			if (!RemoveDirectoryRecursively(fName.c_str()))
			{
				LogError("Error removing dir %s", fName.c_str());
				break;
			}
		}
	}

	free (buf);
	closedir(dp);

	//delete the final dir as well
	rmdir( path.c_str());
	return true; //success
}