Recursively remove a directory using C

Welcome to Programming Tutorial official website. Today - we are going to cover how to solve / find the solution of this error Recursively remove a directory using C on this date .

I want to implement this myself and I come up something like this:

/* DIR *opendir(const char *name);
 *
 * Open a directory stream to argv[1] and make sure
 * it's a readable and valid (directory) */
if ((dip = opendir(argv[1])) == NULL)
{
        perror("opendir");
        return 0;
}

printf("Directory stream is now openn");

/*  struct dirent *readdir(DIR *dir);
 *
 * Read in the files from argv[1] and print */
while ((dit = readdir(dip)) != NULL)
{
        printf("n%s", dit->d_name);
        remove(dit->d_name);
}

I am guessing I have to start delete things from the deepest level and then go up, but now it seems readdir does not give me that order.

Please help, thanks!

Answer

You have to recurse all the way down to the lower levels first, as in the following pseudo code:

def delDir (d):
    # Delete the directories by recursion, files straight away.

    for every item in d:
        if item is a directory:
            delDir (item)
        else:
            delete file item

    # Finally, remove the directory itself.

    remove directory d

That’s it really, this guarantees that all lower directories and files are deleted before you attempt to remove the parent.


Since item is likely to be just the current component of the directory, you may need to construct a full name using, for example, strcpy/strcat:

def delDir (dirName):
    def localBuff[enoughSpace]

    # Delete the directories by recursion, files straight away.

    dirHandle = dirOpen (dirName)
    itemName = dirnext (dirHandle)
    while itemName is valid:
        if itemName <> "." and itemName <> "..":
            strcpy dirName to localBuff
            strcat "/" to localBuff
            strcat itemName to localBuff

            if itemName is a directory:
                delDir (localBuff)
           else:
                delete file localBuff

        itemName = dirnext (dirHandle)

    dirClose (dirHandle)

    # Finally, remove the directory itself.

    remove directory dirName