Skip to content

Instantly share code, notes, and snippets.

@AlexsJones
Created July 17, 2012 08:59
Show Gist options
  • Select an option

  • Save AlexsJones/3128195 to your computer and use it in GitHub Desktop.

Select an option

Save AlexsJones/3128195 to your computer and use it in GitHub Desktop.
int jnx_system::recursive_delete(std::string path)
{
DIR *dp;
struct dirent *ep;
char *dirname=new char[path.size()+1];
dirname[path.size()]=0;
memcpy(dirname,path.c_str(),path.size());
char abs_filename[FILENAME_MAX];
dp = opendir (dirname);
if (dp != NULL)
{
while (ep = readdir (dp)) {
struct stat stFileInfo;
snprintf(abs_filename, FILENAME_MAX, "%s/%s", dirname, ep->d_name);
if (lstat(abs_filename, &stFileInfo) < 0)
perror ( abs_filename );
if(S_ISDIR(stFileInfo.st_mode)) {
if(strcmp(ep->d_name, ".") &&
strcmp(ep->d_name, "..")) {
printf("%s directory\n",abs_filename);
recursive_delete(abs_filename);
}
} else {
printf("%s file\n",abs_filename);
remove(abs_filename);
}
}
(void) closedir (dp);
}
else
perror ("Couldn't open the directory");
remove(dirname);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment