1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include"windows.h"
#include <stdio.h>
#include <string>
BOOL IsDirectory(const char* pDir)
{
char szCurPath[500];
ZeroMemory(szCurPath, 500);
sprintf_s(szCurPath, 500, "%s//*", pDir);
WIN32_FIND_DATAA FindFileData;
ZeroMemory(&FindFileData, sizeof(WIN32_FIND_DATAA));
HANDLE hFile = FindFirstFileA(szCurPath, &FindFileData); /**< find first file by given path. */
if (hFile == INVALID_HANDLE_VALUE)
{
FindClose(hFile);
return FALSE; /** 如果不能找到第一个文件,那么没有目录 */
}
else
{
FindClose(hFile);
return TRUE;
}
}
BOOL DeleteDirectory(const char* DirName)
{
// CFileFind tempFind; //声明一个CFileFind类变量,以用来搜索
char szCurPath[MAX_PATH]; //用于定义搜索格式
_snprintf(szCurPath, MAX_PATH, "%s//*.*", DirName); //匹配格式为*.*,即该目录下的所有文件
WIN32_FIND_DATAA FindFileData;
ZeroMemory(&FindFileData, sizeof(WIN32_FIND_DATAA));
HANDLE hFile = FindFirstFileA(szCurPath, &FindFileData);
BOOL IsFinded = TRUE;
while (IsFinded)
{
IsFinded = FindNextFileA(hFile, &FindFileData); //递归搜索其他的文件
if (strcmp(FindFileData.cFileName, ".") && strcmp(FindFileData.cFileName, "..")) //如果不是"." ".."目录
{
std::string strFileName = "";
strFileName = strFileName + DirName + "//" + FindFileData.cFileName;
std::string strTemp;
strTemp = strFileName;
if (IsDirectory(strFileName.c_str())) //如果是目录,则递归地调用
{
printf("目录为:%s/n", strFileName.c_str());
DeleteDirectory(strTemp.c_str());
}
else
{
DeleteFileA(strTemp.c_str());
}
}
}
FindClose(hFile);
BOOL bRet = RemoveDirectoryA(DirName);
if (bRet == 0) //删除目录
{
printf("删除%s目录失败!/n", DirName);
return FALSE;
}
return TRUE;
}