카테고리

  • 안드로이드
  • IOS
  • MFC
  • JAVA
  • AWS
  • LAMP
  • 여행&사진
  • 이런저런생활
  • 2015년 11월 13일 금요일

    MFC 폴더 복사 및 파일 복사

    SHFileOperation 함수를 이용하여 디렉토리를 복사 할 수 있도록 하려고 했지만 SHFileOperation함수는 Vista이전 버젼에서만 가능!!


    IFileOperation을 이용하라고 해서 해보려 했지만 생각보다 사용 방법이 너무 어려움.
    따라서!
    그냥 내가 만듬

    //** Parameter defPath : 복사 대상, copyTo : 복사할 장소
    //** defPath = C:\GRTMDB\0011 <-폴더 및 파일이 있음
    //** copyTo = C:\GRTMDB\0012 <-0012폴더부터 생성할 예정
    void CMDBAGDlg::CopyModelDB(CString defPath, CString copyTo)
    {
      CFileFind cFileFinder;
      bol bResult;
      bResult = cFileFinder.FindFile(defPath + _T("*.*"));

      while(bResult) {
        bResult = cFileFinder.FindNextFile();
        CString fName = cFileFinder.GetFileName();

        if(fName.Compare(_T(".")) == 0 ||
            fName.Compare(_T("..")) == 0 ||
            fName.Compare(_T("Thumbs.db")) == 0) continue;

        if(cFileFinder.IsDirectory() == true) {
          CreateDirectory(copyTo + _T("\\") + fName, NULL);
          //** 재귀함수를 통해 하위 폴더 확인
          CopyModelDB(defPath + _T("\\") + fName, copyTo + _T("\\") + fName);
        } else if(cFileFinder.IsArchived()) {
          //** 파일복사
          ::CopyFile(defPath + _T("\\") + fName, copyTo + _T("\\") + fName, FALSE);
        }
      }
    }

    2015년 11월 11일 수요일

    MFC 폴더 생성 및 삭제

    출처 : http://mooyou.tistory.com/29


    // 디렉토리 생성
    // 디렉토리 생성 성공: TRUE, 실패: FALSE 반환
    // 함수 사용예: CreateDir(_T("C:\\dir_name\\"));
    BOOL CreateDir(CString dir)
    {
        CFileFind file;
        CString strFile = _T("*.*");
        BOOL bResult = file.FindFile(dir + strFile);
        if(!bResult)
        {
            bResult = CreateDirectory(dir, NULL);
        }
        return bResult;
    }


    // 디렉토리 삭제
    // 디렉토리내에 존재하는 하위 폴더 및 모든 파일 삭제
    // 함수 사용예: DeleteDir(_T("C:\\dir_name\\*.*"));

    BOOL DeleteDir(CString dir)
    {
        if(dir == _T(""))
        {
            return FALSE;
        }
        BOOL bRval = FALSE;
        int nRval = 0;
        CString szNextDirPath = _T("");
        CString szRoot = _T("");
        CFileFind find;
        // Directory가 존재 하는지 확인 검사
        bRval = find.FindFile(dir);
        if(bRval == FALSE)
        {
            return bRval;
        }
        while(bRval)
        {
            bRval = find.FindNextFile();
            // . or .. 인 경우 무시한다.
            if(find.IsDots() == TRUE)
            {
                continue;
            }
            // Directory 일 경우
            if(find.IsDirectory())
            {
                szNextDirPath.Format(_T("%s\\*.*"), find.GetFilePath());
                // Recursion function 호출
                DeleteDir(szNextDirPath);
            }
            // file일 경우
            else
            {
                //파일 삭제
                ::DeleteFile(find.GetFilePath());
            }
        }
        szRoot = find.GetRoot();
        find.Close();
        Sleep(1);
        bRval = RemoveDirectory(szRoot);
        return bRval;
    }