/***********************************************************************
* deleteFile.h
* 删除一个目录下的文件
****************/
extern "C" {
void DelFileW(char *cFilePath);
void DelFileA(char *cFilePath);
}
#include
#include
#include
#include "deleteFile.h"
using namespace std;
#ifdef UNICODE
void DelFileW(char *cFilePath)
{
WIN32_FIND_DATA data;
HANDLE hFind;
WCHAR cFullPath[MAX_PATH] = { 0 };
WCHAR cNewPath[MAX_PATH] = { 0 };
char NewPath[MAX_PATH] = { 0 };
char FullPath[MAX_PATH] = { 0 };
wchar_t *ch_dot = L".";
wchar_t *ch_double_dot = L"..";
MultiByteToWideChar(CP_ACP, 0, cFilePath, strlen(cFilePath) + 1, cFullPath,
sizeof(cFullPath) / sizeof(cFullPath[0]));
wsprintf(cFullPath, L"%s\\*.*", cFullPath);
hFind = FindFirstFile(cFullPath, &data);
do
{
if ((!wcscmp(ch_dot, data.cFileName)) || (!wcscmp(ch_double_dot, data.cFileName)))
{
continue;
}
if (data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
MultiByteToWideChar(CP_ACP, 0, cFilePath, strlen(cFilePath) + 1, cFullPath,
sizeof(cFullPath) / sizeof(cFullPath[0]));
wsprintf(cNewPath, L"%s\\%s", cFullPath, data.cFileName);
WideCharToMultiByte(CP_ACP, 0, cNewPath, -1,
NewPath, MAX_PATH, NULL, NULL);
DelFileW(NewPath);
}
MultiByteToWideChar(CP_ACP, 0, cFilePath, strlen(cFilePath) + 1, cFullPath,
sizeof(cFullPath) / sizeof(cFullPath[0]));
wsprintf(cFullPath, L"%s\\%s", cFullPath, data.cFileName);
DeleteFile(cFullPath);
} while (FindNextFile(hFind, &data));
MultiByteToWideChar(CP_ACP, 0, cFilePath, strlen(cFilePath) + 1, cFullPath,
sizeof(cFullPath) / sizeof(cFullPath[0]));
RemoveDirectory(cFullPath);
FindClose(hFind);
}
#else
void DelFileA(char *cFilePath)
{
WIN32_FIND_DATA data;
HANDLE hFind;
char NewPath[MAX_PATH] = { 0 };
char FullPath[MAX_PATH] = { 0 };
const char *ch_dot = ".";
const char *ch_double_dot = "..";
sprintf(FullPath, "%s\\*.*", cFilePath);
hFind = FindFirstFile(FullPath, &data);
do{
if ((!strcmp(ch_dot, data.cFileName)) || (!strcmp(ch_double_dot, data.cFileName)))
{
continue;
}
if (data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
sprintf(NewPath, "%s\\%s", cFilePath, data.cFileName);
DelFileA(NewPath);
}
sprintf(FullPath, "%s\\%s", cFilePath, data.cFileName);
DeleteFile(FullPath);
} while (FindNextFile(hFind, &data));
RemoveDirectory(cFilePath);
FindClose(hFind);
}
#endif