tags:

views:

126

answers:

4

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 ...

+6  A: 

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.

Jerry Coffin
+1  A: 

You'll need to loop over all the files in the directory and delete those first. The code is platform dependent though (as others have mentioned).

For example the code on this MSDN page (from which this is extracted so there will be undefined variables) will work for Windows, but not Unix/Linux:

HANDLE hFind = FindFirstFile(szDir, &ffd);

if (INVALID_HANDLE_VALUE == hFind) 
{
    DisplayErrorBox(TEXT("FindFirstFile"));
    return dwError;
} 

// List all the files in the directory with some info about them.
do
{
    if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
        _tprintf(TEXT("  %s   <DIR>\n"), ffd.cFileName);
    }
    else
    {
        filesize.LowPart = ffd.nFileSizeLow;
        filesize.HighPart = ffd.nFileSizeHigh;
        _tprintf(TEXT("  %s   %ld bytes\n"), ffd.cFileName, filesize.QuadPart);
    }
}
while (FindNextFile(hFind, &ffd) != 0);

dwError = GetLastError();
if (dwError != ERROR_NO_MORE_FILES) 
{
    DisplayErrorBox(TEXT("FindFirstFile"));
}

FindClose(hFind);

prints the file information, but adapting it to delete shouldn't be too hard.

You'll need to call this recursively for all sub directories in the tree.

ChrisF
+1  A: 

First of all, any file i/o -- particularly directory changes, is very much OS dependent.

But, for the most part, it's a) deleted the files, then b) remove the directory. (any shortcut to that would definitely be OS dependent, and often OS version depedent)

James Curran
+1  A: 

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);
}
bta