• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

how can i delete a directory?

Anyone can give me a sample code
to delete a directory(include subdir)
,the input parametric is the name of
the directory,
Thank u?
1 Solution
well, the easy (cheating) way might be to call system() and ask it to deltree the directory :-)

Not as slick as writing recursive functions, I agree, but it offloads the problem onto someone else. Might be the quickest thing to get debugged & working ?
For which OS? There will be no ansi function which will solve this!
i see you got one idea but if you want to use only c\c++ you can do the next

first for deleting a dir you have to delete all the files in it
so what you have to do is make a recorsive function that will that will go to each dir in your start dir and delete all the files there and when it returns it delete the dir itself

the run it on your dir

it will look like that

all the dirs
you want to delete windows it will go
to windows-->system-->files
delete all the files in the files dir
delete the files dir
delete all the files in the system dir
delete the system dir
delete all the files in the windows dir
delete windows dir<it should know that you are in the first dir "windows">

that the idea
The IT Degree for Career Advancement

Earn your B.S. in Network Operations and Security and become a network and IT security expert. This WGU degree program curriculum was designed with tech-savvy, self-motivated students in mind – allowing you to use your technical expertise, to address real-world business problems.

To keep your code most compatible with any OS you might want to use the POSIX functions; check out the documentation / man pages for:

  - opendir (3)
  - readdir (3)
  - closedir (3)

  - unlink (2)
  - rmdir (2)
>> - opendir (3)
>> - readdir (3)
>> - closedir (3)

Or, on DOS/Windows/etc:

- _findfirst()
- _findnext()
If you are on a Windows platform, you can use the SHFileOperation() function to delete an entire directory tree.
> - _findfirst()
> - _findnext()

Do the C libraries under DOS/Windows not contain POSIX functions?
//next code works as in Windows, as in Dos:

#include <cderr.h>
#include <string.h>
#include <dir.h>
#include <sys\stat.h>

void ClearDir(char *szFind)
  int done;
  char szLocalFind[255];

  strcpy(szLocalFind,szFind); strcat(szLocalFind,"\\*.*");
                              //delete all file
  struct ffblk ffblk;
  done = findfirst(szLocalFind,&ffblk,
                   FA_ARCH | FA_SYSTEM | FA_HIDDEN);
  while (!done)                  //files
      char szName[255];
      strcpy(szName, szFind); strcat(szName,"\\");
      if (!unlink(szName))            //read only!
        chmod(szName,S_IREAD | S_IWRITE);
      done = findnext(&ffblk);
  done = findfirst(szLocalFind,&ffblk,FA_DIREC);//all subdirs
  while (!done)
    {                  //now dir:
      char szName[255];
      strcpy(szName, szFind); strcat(szName,"\\");
      if (*ffblk.ff_name != '.' && !rmdir(szName))
        ClearDir(szName);            //delete all sub dir
        rmdir(szName);      //must be empty!
      done = findnext(&ffblk);
I hope, it helps. Alex
MS does not supply the functions in any of their compilers.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now