Solved

how can i delete a directory?

Posted on 2000-04-16
9
178 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

809 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