Remove A Directory
// 23.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <windows.h>
bool RemoveCompleteDirectory(char* path);
int main(int argc, char* argv[])
{
char* temp=0;
temp = argv[1];
if(argc !=2){
printf("parameter error!\n");
exit(1);
}
while ( strcmp(temp,"-q")!=0 ){
printf("RemoveDirectory %s ......\n",temp);
if (RemoveCompleteDirectory(temp))
printf("\nDone successfully!\n");
printf("You can input \"-q\" to quit\n");
printf("Input other path: ");
gets(temp);
}
return 0;
}
bool RemoveCompleteDirectory(char* path)
{
RemoveDirectory(path);
WIN32_FIND_DATA find; HANDLE hndle;
char *strFindFiles = (char*) malloc(sizeof(char)*(strlen(path)+4));
strcpy(strFindFiles,path);
strcat(strFindFiles,"\\*.*");
hndle = FindFirstFile(strFindFiles,&find);
while (hndle != INVALID_HANDLE_VALUE)
{
char *strFolderItem = (char*) malloc(sizeof(char) * (strlen(path)+strlen(find.cFileName)+1));
strcpy(strFolderItem,path);
strcat(strFolderItem,"\\");
strcat(strFolderItem,find.cFileName);
if ((!strcmp(find.cFileName,"."))||(!strcmp(find.cFileName,"..")))
{
RemoveDirectory(strFolderItem);
if (FindNextFile(hndle,&find))
{
continue;
}
else
{
RemoveDirectory(path);
break;
}
}
if (find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
RemoveCompleteDirectory(strFolderItem);
}
else
{
SetFileAttributes(strFolderItem,FILE_ATTRIBUTE_NORMAL);
if (DeleteFile(strFolderItem)) return false;
}
if (FindNextFile(hndle,&find))
{
continue;
}
else
{
break;
}
}
FindClose(hndle);
SetFileAttributes(path,FILE_ATTRIBUTE_DIRECTORY);
RemoveDirectory(path);
return true;
}