|
//红色字体的就是执行不了的,不断点调试的话发现不了。。。
BOOL Delete_File(LPCTSTR lpszFolderPath)
{
CString temp=lpszFolderPath;
if (!lpszFolderPath || temp.GetLength()<= 0)return FALSE;
TCHAR szFindPath[MAX_PATH] = { 0 },szOriginPath[MAX_PATH]={0};
wcscpy(szFindPath, lpszFolderPath);
if (szFindPath[_tcslen(szFindPath) - 1] == '\\')szFindPath[wcslen(szFindPath) - 1] = '\0';
_stprintf(szOriginPath,szFindPath);
WIN32_FIND_DATA FindData = { 0 };
HANDLE pFind = FindFirstFile(szFindPath, &FindData);
if (pFind == INVALID_HANDLE_VALUE) return FALSE;
while (FindNextFile(pFind,&FindData))
{
if (FindData.cFileName[0] == '.') continue;//过滤.和..
TCHAR szSubPath[MAX_PATH] = { 0 };
_stprintf(szSubPath,L"%s\\%s",szOriginPath,FindData.cFileName);
if (FindData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
//如果是文件夹,进行递归
Delete_File(szSubPath);
RemoveDirectory(szSubPath);
}
else
{
//是文件
DeleteFile(szSubPath);
}
}
FindClose(pFind);
return TRUE;
}
本帖最后由 yoobaby 于 2022-7-8 15:01 编辑
跟那个红色的字应该没啥关系的,你的逻辑看得有点懵。大概改了下
主要懵的点是,你查找文件,从头到尾没看到你要查找的文件名,也没有类似*.*这样的通匹文件名。
你注意下 szFileName这个变量
- BOOL Delete_Files(LPCTSTR lpszFolderPath, LPCTSTR szFileName = TEXT("*.*"))
- {
- CString temp = lpszFolderPath;
- if (!lpszFolderPath || temp.GetLength() <= 0)return FALSE;
- TCHAR szFindPath[MAX_PATH] = { 0 };
- TCHAR szOriginPath[MAX_PATH] = { 0 };
- _tcscpy(szFindPath, lpszFolderPath);
- size_t pathLen = _tcslen(szFindPath);
- if (szFindPath[pathLen - 1] != '\\')
- _tcscat(szFindPath, TEXT("\")); // 这边是两个\ ,为知道为什么论坛会转义掉
- _tcscpy(szOriginPath, szFindPath);
- _tcscat(szFindPath, szFileName);
- WIN32_FIND_DATA FindData = { 0 };
- HANDLE pFind = FindFirstFile(szFindPath, &FindData);
- if (pFind == INVALID_HANDLE_VALUE) return FALSE;
- while (FindNextFile(pFind, &FindData))
- {
- if (FindData.cFileName[0] == '.') continue;
- TCHAR szSubPath[MAX_PATH] = { 0 };
- _stprintf(szSubPath, L"%s%s", szOriginPath, FindData.cFileName);
- if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
- {
- Delete_Files(szSubPath);
- RemoveDirectory(szSubPath);
- }
- else
- {
- DeleteFile(szSubPath);
- }
- }
- FindClose(pFind);
- return TRUE;
- }
- //调用
- Delete_Files(L"T:\\test");
复制代码
|
-
执行到这会跳转至第二张截图那个界面
-
第二张截图
上一篇: 为什么多线程和单线程返回的父窗口指针不一样?下一篇: 2010版本 中CreateFile等关键词不高亮,怎么设置
|