Delete folder and all files/subdirectories

user95644 picture user95644 · Jul 19, 2009 · Viewed 29.3k times · Source

How can I delete a folder with all it's files/subdirectories (recursive deletion) in C++?

Answer

William Pursell picture William Pursell · Jul 19, 2009

Seriously:

system("rm -rf /path/to/directory")

Perhaps more what you're looking for, but unix specific:

/* Implement system( "rm -rf" ) */
    
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/stat.h>
#include <ftw.h>
#include <unistd.h>

/* Call unlink or rmdir on the path, as appropriate. */
int
rm(const char *path, const struct stat *s, int flag, struct FTW *f)
{
        int status;
        int (*rm_func)(const char *);
        (void)s;
        (void)f;
        rm_func = flag == FTW_DP ? rmdir : unlink;
        if( status = rm_func(path), status != 0 ){
                perror(path);
        } else if( getenv("VERBOSE") ){
                puts(path);
        }
        return status;
}


int
main(int argc, char **argv)
{
        (void)argc;
        while( *++argv ) {
                if( nftw(*argv, rm, OPEN_MAX, FTW_DEPTH) ){
                        perror(*argv);
                        return EXIT_FAILURE;
                }
        }
        return EXIT_SUCCESS;
}