フォルダを削除するにはRemoveDirectoryを使うのだけれど、フォルダを空にしておかないと使えない。
なのでこういった関数を自作する必要がある
// lpszFolderPath 削除するフォルダのフルパス
BOOL DeleteFolder(LPCTSTR lpszFolderPath)
{
CFileFind fFind;
// 検索用のワイルドカード付きパスを用意
CString strFolderPath = lpszFolderPath;
strFolderPath.TrimRight(_T("\\"));
strFolderPath += _T("\\*.*");
if(!fFind.FindFile(strFolderPath))
return FALSE;
BOOL bRet = TRUE;
while(bRet)
{
bRet = fFind.FindNextFile();
if(fFind.IsDots())
continue;
// GetFilePath を使うと
// 正常にパスが取得できない場合があるので
// フルパスを作成する処理を入れる
CString strPath;
strPath.Format
(_T("%s\\%s"),lpszFolderPath, fFind.GetFileName());
// フォルダがあれば再帰
// ファイルなら削除
if(fFind.IsDirectory())
DeleteFolder(strPath);
else
DeleteFile(strPath);
}
fFind.Close();
return RemoveDirectory(lpszFolderPath);
}ネタ元
参考
![]()