文件操作些許方法實現

1)將已有文件夾中的文件拷貝至另一個文件夾,實現方法如下所示:
BOOL CopyDirectory(CString strTargetDir, CString strSourceDir, BOOL bFilterDotName)
{
    if(strTargetDir.IsEmpty() || strSourceDir.IsEmpty())
    {
        return FALSE;
    }
    //CHN 源路徑存在的情況下,才做以下操作
    if (PathFileExists(strSourceDir))
    {
        //CHN 將刪除動作提前,避免執行覆蓋操作時,容量判斷不準
        if (PathFileExists(strTargetDir))
        {
            DeleteFiles(strTargetDir+_T('\0'));//CHN 清空原有文件
        }
        CreateDirectoryOfAll(strTargetDir);

        //CHN 判斷是否分區容量是否允許拷貝
        CString strDisk = strTargetDir.Left(2); //CHN 分區容量
        UINT64 nDiskFreeSize = GetDiskFreeSpace(strDisk);
        UINT64 nFolderSize = 0;
        GetFolderContainerSize(strSourceDir, nFolderSize);
        if(nDiskFreeSize - DISK_REMAIN_CONST < nFolderSize)
        {
            PRDEMessageBox(ERPDE_HDD_FULL, MB_OK|MB_ICONERROR, NULL, EPRDE_MSGBOX_TITLE_ERROR);
            return FALSE;
        }


        CFileFind fFinder;

        //CHN 該文件下所有的文件
        CString strDirExe = strSourceDir +  _T("\\*.*"); 

        BOOL bFind = fFinder.FindFile(strDirExe);
        while(bFind)
        {
            bFind = fFinder.FindNextFile();
            CString strName = fFinder.GetFileName();
            //CHN 目標文件的路徑
            strDirExe = strTargetDir + _T("\\") + fFinder.GetFileName();
            if(fFinder.IsDots())
            {
                continue;
            }
            if (bFilterDotName && strName.GetAt(0)== '.')
            {
                continue;
            }
            else if(fFinder.IsDirectory())
            {
                //CHN 創建對應的文件夾
                CreateDirectory(strDirExe, NULL);
                //CHN 將對應的內容拷貝到該文件夾下
                CopyDirectory(strDirExe, fFinder.GetFilePath(), bFilterDotName);
            }       
            else
            {
                //CHN 拷貝對應的文件
                CopyFile(fFinder.GetFilePath(), strDirExe, FALSE);
            }

            if(!bFind)
            {
                break;
            }
        }

        fFinder.Close();
    }

    return TRUE;
}

(2)對文件的路徑移動操作,其方法實現:
BOOL MoveDirectory(CString strTargetDir, CString strSourceDir, int* pnFileCnt)
{
    if (strTargetDir == strSourceDir)
    {
        *pnFileCnt = 0;
        return TRUE;
    }

    if(strTargetDir.IsEmpty() || strSourceDir.IsEmpty())
    {
        return FALSE;
    }

    //CHN 將刪除動作提前,避免執行覆蓋操作時,容量判斷不準
    if (PathFileExists(strTargetDir))
    {
        DeleteFiles(strTargetDir+_T('\0'));//CHN 清空原有文件
    }

    //CHN 判斷是否分區容量是否允許拷貝
    CString strDisk = strTargetDir.Left(2); //CHN 分區容量
    CString strsorDisk =  strSourceDir.Left(2);
    strDisk.MakeUpper();
    strsorDisk.MakeUpper();
    if(strDisk.Compare(strsorDisk) != 0)
    {      
        UINT64 nDiskFreeSize = GetDiskFreeSpace(strDisk);
        UINT64 nFolderSize = 0;
        GetFolderContainerSize(strSourceDir, nFolderSize);
        if(nDiskFreeSize - DISK_REMAIN_CONST < nFolderSize)
        {
    PRDE_BROADCAST_MESSAGE(WM_PRDE_MOVE_DIRECTORY_DISK_FULL,0,0);
            return FALSE;
        } 
    }
    CreateDirectoryOfAll(strTargetDir);//CHN 創建文件夾,放到判斷容量之後,避免容量不足時殘留空文件夾

    CFileFind fFinder;

    //CHN 該文件下所有的文件
    CString strDirExe = strSourceDir +  _T("\\*.*"); 

    BOOL bFind = fFinder.FindFile(strDirExe);
    while(bFind)
    {
        bFind = fFinder.FindNextFile();
        //CHN 目標文件的路徑
        strDirExe = strTargetDir + _T("\\") + fFinder.GetFileName();
        if(fFinder.IsDots())
        {
            continue;
        }
        else if(fFinder.IsDirectory())
        {
            //CHN 創建對應的文件夾
            CreateDirectory(strDirExe, NULL);
            //CHN 將對應的內容拷貝到該文件夾下
            if(!MoveDirectory(strDirExe, fFinder.GetFilePath(), pnFileCnt))
            {
                fFinder.Close();
                return FALSE;
            }
        }
        else
        {
            //CHN 拷貝對應的文件
            //CopyFile(fFinder.GetFilePath(), strDirExe, FALSE);
            BOOL bRet = MoveFileEx(fFinder.GetFilePath(), strDirExe, MOVEFILE_COPY_ALLOWED|MOVEFILE_WRITE_THROUGH);
            ASSERT(bRet);
            if(bRet)
            {
                if (pnFileCnt)
                    (*pnFileCnt)++;
            }
            else
            {
                Error(SIV_LOG_EDITOR_PRODUCTIONMOD_MAROC,_T("$$[MoveFileEx] Failed File Path = %s\n"),fFinder.GetFilePath());
				bRet = CopyFile(fFinder.GetFilePath(), strDirExe, FALSE);
				ASSERT(bRet);
				if(bRet)
				{
					Debug(SIV_LOG_EDITOR_PRODUCTIONMOD_MAROC,_T("$$[CopyFile] Succussed File Path = %s\n"),fFinder.GetFilePath());
                    if (pnFileCnt)
                        (*pnFileCnt)++;
                }
                else
                {
                    fFinder.Close();
                    return FALSE;
                }
            }
            DeleteFile(fFinder.GetFilePath());
        }

        if(!bFind)
        {
            break;
        }
    }

    fFinder.Close();
    return TRUE;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章