[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

how can i delete a directory?

Posted on 2000-04-16
9
Medium Priority
?
187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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.

649 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