menu

秋梦无痕

一场秋雨无梦痕,春夜清风冻煞人。冬来冷水寒似铁,夏至京北蟑满城。

Avatar

RemovePath

递归删除文件夹。

如果不能正确删除,则文件将在系统重起的时候被删除。(注:win98不支持MoveFileEx。)

VOID RemovePath(LPCSTR lpszPath) {
DWORD dwFileAttributes = GetFileAttributes(lpszPath);

if(dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) {
char szAbsPath[MAX_PATH], szFind[MAX_PATH];
strcpy(szAbsPath, lpszPath);
if(lpszPath[strlen(lpszPath) - 1] != '\\') {
strcat(szAbsPath, "\\");
}

strcpy(szFind, szAbsPath);
strcat(szFind, "*");

HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA FindFileData;
hFind = FindFirstFile(szFind, &FindFileData);
if(hFind != INVALID_HANDLE_VALUE) {
if(strcmp(FindFileData.cFileName, ".") != 0
&& strcmp(FindFileData.cFileName, "..") != 0) {
char szFile[MAX_PATH];
strcpy(szFile, szAbsPath);
strcat(szFile, FindFileData.cFileName);
RemovePath(szFile);
}
while (FindNextFile(hFind, &FindFileData) != 0) {
if(strcmp(FindFileData.cFileName, ".") != 0
&& strcmp(FindFileData.cFileName, "..") != 0) {
char szFile[MAX_PATH];
strcpy(szFile, szAbsPath);
strcat(szFile, FindFileData.cFileName);
RemovePath(szFile);
}
}
FindClose(hFind);
}
if(!RemoveDirectory(lpszPath)) {
MoveFileEx(lpszPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
}
} else {
if(!DeleteFile(lpszPath)) {
MoveFileEx(lpszPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
}
}
}

评论已关闭