C++递归删除目录

//枚举一个目录下的目录和文件列表
bool EnumDirectoryContent(CString strDirName , CString strFilter,
CStringArray &arrSubDirNames ,
CStringArray &arrSubFileNames)
{
strDirName.TrimLeft();
strDirName.TrimRight();

if(strDirName.GetLength() == 0)return false;

arrSubDirNames.RemoveAll();
arrSubFileNames.RemoveAll();

CString strDirList , strFileList , strDirFileList;
CFileFind finder;
CString strWildcard;

if(strDirName.GetAt(strDirName.GetLength() – 1) == _T(‘\\’) ||strDirName.GetAt(strDirName.GetLength() – 1) == _T(‘/’))
strWildcard = strDirName + strFilter;
else
strWildcard = strDirName +”\\”+ strFilter;
BOOL bWorking = finder.FindFile(strWildcard);

if(!bWorking)return false;
while(bWorking)
{
bWorking = finder.FindNextFile();

if (finder.IsDots())continue;
if(finder.IsSystem())continue;
if (finder.IsDirectory())
{
arrSubDirNames.Add(finder.GetFileName());
continue;
}

arrSubFileNames.Add(finder.GetFileName());
}
finder.Close();

return true;
}
bool EnumDirectoryContent(CString strDirName ,
CStringArray &arrSubDirNames ,
CStringArray &arrSubFileNames)
{
return EnumDirectoryContent(strDirName,_T(“*.*”),arrSubDirNames,arrSubFileNames);
}
BOOL DeleteDirectory(CString strDwgDir)
{
CStringArray arrSubDirNames;
CStringArray arrSubFileNames;
EnumDirectoryContent(strDwgDir,arrSubDirNames,arrSubFileNames);
for(int i=0;i<arrSubDirNames.GetCount();i++)
{
CString strDir=strDwgDir+_T(“\\”)+arrSubDirNames.GetAt(i);
DeleteDirectory(strDir);
}
for(int i=0;i<arrSubFileNames.GetCount();i++)
{
CString strFile=strDwgDir+_T(“\\”)+arrSubFileNames.GetAt(i);
DeleteFile(strFile);
}
RemoveDirectory(strDwgDir);
return TRUE;
}

此条目发表在C++分类目录,贴了, 标签。将固定链接加入收藏夹。

发表评论