giovedì 7 gennaio 2010

Recursive Dir Removing function

This code remove completely a dir and all the files included.

int DeleteDir(const char* dirn){
DIR* dir;
struct dirent *dirp;
if((dir=opendir(dirn))==NULL){
return -1;
}
struct stat st;
while((dirp=readdir(dir))!=NULL){
char name[256];
sprintf(name, "%s/%s", dirn, dirp->d_name);
if(dirp->d_name[0]!='.'){
stat(name, &st);
if((st.st_mode&S_IFDIR)){
DeleteDir(name);
}else unlink(name);
}
}
rmdir(dirn);
return 0;
}


To use this function you just need to call DeleteDir(pathofthedir);
You need to include:

stdio.h
string.h
dirent.h
sys/stat.h

Nessun commento:

Posta un commento