删除文件夹及其里面所有内容,可能需要修改文件属性

tech2022-09-18  103

void RemoveFolder(PCTSTR szDirPath) { CString strDir(szDirPath); CString strDest(strDir); strDest += _T("\\*.*"); _tfinddata_t findData; intptr_t handle = _tfindfirst(strDest, &findData); // 查找目录中的第一个文件 if (handle != -1) { do { if ((_tcscmp(findData.name, _T(".")) == 0) || (_tcscmp(findData.name, _T("..")) == 0)) // 不为"."或".." continue; CString strFound; strFound.Format(_T("%s\\%s"), (PCTSTR)strDir, findData.name); if (findData.attrib & _A_SUBDIR) { RemoveFolder(strFound); } else { if (findData.attrib & _A_RDONLY) { ::SetFileAttributes(strFound, FILE_ATTRIBUTE_NORMAL); } ::DeleteFile(strFound); } } while (_tfindnext(handle, &findData) == 0); // 查找目录中的下一个文件 } _findclose(handle); ::SetFileAttributes(strDir, FILE_ATTRIBUTE_NORMAL); //设置文件夹的属性 ::RemoveDirectory(strDir); }
最新回复(0)