반응형

[MFC] 폴더 삭제 및 이동

 

 

 

1. 폴더 삭제

BOOL RemoveDir(CString strDir, BOOL bOnlyFile, HWND hWnd)
{
     CFileFind finder;
     BOOL bWorking = TRUE;
     CString strDirFile = strDir + CString(_T("\\*.*"));

 

     if (FindFile(strDir, TRUE) == false)
     {
          return FALSE;
     }

 

     bWorking = finder.FindFile(strDirFile);
     while (bWorking)
     {
          bWorking = finder.FindNextFileW();

 

          if (finder.IsDots())
               continue;

 

          if (finder.IsDirectory() && !bOnlyFile)
               RemoveDir(finder.GetFilePath(), false, hWnd);
          else
               ::DeleteFile(finder.GetFilePath());
     }


     finder.Close();

 

     if (!bOnlyFile)
          ::RemoveDirectory(strDir);

 

     return TRUE;
}

 

 

2. 폴더 이동

BOOL MoveFiles(LPCTSTR strFrom, LPCTSTR strTo)
{
     CFileFind finder;
     BOOL bRet = TRUE, bWorking = TRUE;
     CString strDirFile = strFrom + CString(_T("\\*.*"));
     CString strSubFrom, strSubTo;

 

     if (FindFile(strFrom, TRUE) == false)
     {
          return FALSE;
     }

 

if (!FindFile(strTo, TRUE))
      CreateDirectories(strTo);

 

bWorking = finder.FindFile(strDirFile);
while (bWorking)
{
    bWorking = finder.FindNextFileW();

          

if (finder.IsDots())
    continue;

  

strSubFrom.Format(_T("%s"), finder.GetFilePath());
strSubTo.Format(_T("%s\\%s"), strTo, finder.GetFileName());
if (finder.IsDirectory())
{
    MoveFiles(strSubFrom, strSubTo);
}
else
{
    strSubTo.Replace(_T(".tmp"), _T(""));
    bRet &= MoveFile(strSubFrom, strSubTo);
}

}
finder.Close();

 

RemoveDir(strFrom);

return bRet;

}

728x90
반응형