?
Solved

how can i delete a directory?

Posted on 2000-04-16
9
Medium Priority
?
188 Views
Last Modified: 2010-04-15
Anyone can give me a sample code
to delete a directory(include subdir)
,the input parametric is the name of
the directory,
Thank u?
0
Comment
Question by:choudes
9 Comments
 
LVL 6

Expert Comment

by:graham_k
ID: 2721690
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 ?
0
 
LVL 10

Expert Comment

by:rbr
ID: 2721982
For which OS? There will be no ansi function which will solve this!
0
 
LVL 1

Expert Comment

by:ntdragon
ID: 2722000
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
c:\windows\system\files
you want to delete windows it will go
to windows-->system-->files
delete all the files in the files dir
system<--
delete the files dir
delete all the files in the system dir
windows<--
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:fremsley
ID: 2722084
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)
0
 
LVL 11

Expert Comment

by:alexo
ID: 2722413
>> - opendir (3)
>> - readdir (3)
>> - closedir (3)

Or, on DOS/Windows/etc:

- _findfirst()
- _findnext()
0
 
LVL 32

Expert Comment

by:jhance
ID: 2722553
If you are on a Windows platform, you can use the SHFileOperation() function to delete an entire directory tree.
0
 
LVL 2

Expert Comment

by:fremsley
ID: 2722782
> - _findfirst()
> - _findnext()

Do the C libraries under DOS/Windows not contain POSIX functions?
0
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 300 total points
ID: 2722993
//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,"\\");
      strcat(szName,ffblk.ff_name);
      if (!unlink(szName))            //read only!
      {
        chmod(szName,S_IREAD | S_IWRITE);
        unlink(szName);
      }
      done = findnext(&ffblk);
    }
  done = findfirst(szLocalFind,&ffblk,FA_DIREC);//all subdirs
  while (!done)
    {                  //now dir:
      char szName[255];
      strcpy(szName, szFind); strcat(szName,"\\");
      strcat(szName,ffblk.ff_name);
      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
0
 
LVL 11

Expert Comment

by:alexo
ID: 2723004
MS does not supply the functions in any of their compilers.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question