Solved

how can i delete a directory?

Posted on 2000-04-16
9
176 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now