메뉴 바로가기

서브메뉴 바로가기

본문 바로가기

logo

C/C++/MFC

FILE 디렉토리(폴더) 전체의 파일 복사 SHFileOperation

2006.05.23 13:37

WAT_운영자 조회 수:28552

SHFileOperation()을 써서 파일복사 방법
AAA라는 폴더안의 파일을 BBB폴더로 다 복사를 해보자.

AAA폴더는 m_strDisk의 변수로 폴더명을 받고 ===>예)"C:\AAA"

BBB폴더는 m_strPCfolder의 변수로 폴더명을 받고 ===>예)"C:\BBB"


    sprintf(NewPath,"%s",m_strPCfolder);
    sprintf(OldPath,"%s",m_strDisk);

    ZeroMemory(&shfo, sizeof shfo);
    shfo.hwnd = AfxGetMainWnd()->m_hWnd;
    shfo.wFunc = FO_COPY;

    shfo.fFlags = FOF_SIMPLEPROGRESS|FOF_MULTIDESTFILES ;
    shfo.lpszProgressTitle=pszTitle;
    shfo.pTo=NewPath;
    shfo.pFrom=OldPath;
    int ret = SHFileOperation(&shfo);

그런데 이렇게 하면 "BBB폴더내에 BBB가 이미 존재합니다"라는 메세지를 보이거나
          BBB폴더 내에 AAA폴더가 생성됩니다.



SHFileOperation()을 써서 파일이동 방법
================================================================================

ex)  NewPath = "C:\AAA\*.*";
       shfo.pTo=NewPath;
============================================================================================================

파일 이동 함수
strPath1를  strPath2로 이동시키는 소스입니다.

 

 


BOOL CLeftView::FileMove(CString strPath1, CString strPath2)
{
          SHFILEOPSTRUCT    shfo;
           WORD        wfunc;
           TCHAR        pszTo[1024] = {0};
           TCHAR        pszFrom[1024] = {0};
           TCHAR        pszTitle[MAX_PATH] = {0};
           wfunc = FO_MOVE;
           memset(pszTo, 0, sizeof(pszTo));   // 중요
           memset(pszFrom, 0, sizeof(pszFrom));
           strcpy(pszTitle, "파일이동");
           strcpy(pszTo, strPath2);
           strcpy(pszFrom , strPath1);
   ZeroMemory(&shfo, sizeof(shfo));
   shfo.hwnd = AfxGetMainWnd()->m_hWnd;
   shfo.wFunc = wfunc;
   shfo.lpszProgressTitle=pszTitle;
   shfo.pTo=pszTo;
   shfo.pFrom=pszFrom;

           int ret = SHFileOperation(&shfo);
           return shfo.fAnyOperationsAborted;