文件夹删除:
bool DeleteFolder(string filepath)
{
int len = (int)filepath.length();
int i = 0;
for (auto it = filepath.end(); it != filepath.begin(); )
{
if ('\\' == *(--it))
{
i++;
break;
}
i++;
}
string path = filepath.substr(0,len - i);
CString DirName = path.c_str();
CString PUBPATH;
PUBPATH = DirName;
CFileFind tempFind;
DirName += "\\*.*";
BOOL IsFinded = (BOOL)tempFind.FindFile(DirName);
while(IsFinded)
{
IsFinded = (BOOL)tempFind.FindNextFile();
if(!tempFind.IsDots())
{
string strDirName;
strDirName += PUBPATH;
strDirName += "\\";
strDirName += tempFind.GetFileName();
if(tempFind.IsDirectory())
{
DeleteFolder(strDirName + "\\");
}
else
{
SetFileAttributes(_T(strDirName.c_str()),FILE_ATTRIBUTE_NORMAL);
DeleteFile(_T(strDirName.c_str()));
}
}
}
tempFind.Close();
if(!RemoveDirectory(PUBPATH))
{
return false ;
}
return true;
}
</pre><strong>文件夹复制:</strong></div><div><pre name="code" class="cpp">void CopyFileDirectory(CString source, CString target)
{
CreateDirectory(target, NULL);
CFileFind finder;
CString path;
path.Format("%s/*.*", source);
BOOL haveFile = finder.FindFile(path);
while(haveFile)
{
haveFile = finder.FindNextFile();
if(finder.IsDirectory() && !finder.IsDots())
{
CopyFileDirectory(finder.GetFilePath(), target + "/" + finder.GetFileName());
}
else
{
CopyFile(finder.GetFilePath(), target + "/" + finder.GetFileName(), FALSE);
}
}
}