Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a non-empty directory in C++?

Tags:

c++

file

file-io

In C++, how can I remove a directory with all its contained files? I know there is rmdir, but it will only remove non-empty directories, so how do I list and remove all contained files first?

I know it shouldn't be hard using Boost Filesystem, but I kind of want to avoid building and depending on it just for this one little task ...

like image 231
Frank Avatar asked Dec 07 '22 02:12

Frank


2 Answers

Yes, you normally have to remove the contents first. If you don't want to use Boost for this, you're pretty much stuck with writing non-portable code to find all the files (e.g., FindFirstFile, FindNextFile on Windows, opendir, readdir on Unix and similar) recursively, and remove all of them.

On Windows, you can also use ShFileOperation or the IFileOperation interface. These can handle a recursive delete internally, so you just give it the name of the directory you want removed, and it handles the rest.

As with most COM things, the IFileOperation interface seems to be designed specifically to be as clumsy as possible (e.g., IFileOperation::DeleteItem doesn't actually delete anything--it just adds an item to a list of things to be deleted. Then you have to call IFileOperation::PerformOperations to do the actual deletion.

like image 174
Jerry Coffin Avatar answered Dec 09 '22 14:12

Jerry Coffin


You can use the following code to delete a non-empty directory. This uses Unix-style commands but can be compiled for Windows using Cygwin (if you don't mind depending on the Cygwin DLL).

void delete_folder_tree (const char* directory_name) {
    DIR*            dp;
    struct dirent*  ep;
    char            p_buf[512] = {0};

    dp = opendir(directory_name);

    while ((ep = readdir(dp)) != NULL) {
        sprintf(p_buf, "%s/%s", directory_name, ep->d_name);
        if (path_is_directory(p_buf))
            delete_folder_tree(p_buf);
        else
            unlink(p_buf);
    }

    closedir(dp);
    rmdir(directory_name);
}

int path_is_directory (const char* path) {
    struct stat s_buf;

    if (stat(path, &s_buf))
        return 0;

    return S_ISDIR(s_buf.st_mode);
}
like image 33
bta Avatar answered Dec 09 '22 16:12

bta