(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;
}
文件操作些许方法实现
最新推荐文章于 2024-07-13 17:06:45 发布