16,472
社区成员
发帖
与我相关
我的任务
分享
void COperation::copyFiles(CString strSrcPath,CString strDesPath)
{
CFileFind ff;
CString szDir = strSrcPath;
if(szDir.Right(1) != "\\")
szDir += "\\";
szDir +="*.*";
BOOL bFileExit = ff.FindFile(szDir);
while(bFileExit)
{
bFileExit = ff.FindNextFile();
CString tempSrcPath = ff.GetFilePath();
CString strTitle = ff.GetFileTitle();//得到目录名
CString tempDesPath = tempSrcPath;
tempDesPath.Replace(strSrcPath,strDesPath);
if(ff.IsDirectory() && !ff.IsDots())//当查找的是文件夹时,创建文件夹递归
{
CreateDirectory(tempDesPath,NULL);//创建新的文件夹
copyFiles(tempSrcPath,tempDesPath);//递归
}
else
if( !ff.IsDirectory() && !ff.IsDots())//当查找的是文件时,直接复制
{
CString tempSrcPath = ff.GetFilePath();
CopyFile(tempSrcPath,tempDesPath,false);
}
}
}
void COperation::copyFiles(CString strSrcPath,CString strDesPath)
{
CFileFind ff;
CString szDir = strSrcPath;
if(szDir.Right(1) != "\\")
szDir += "\\";
szDir +="*.*";
BOOL bFileExit = ff.FindFile(szDir);
while(bFileExit)
{
bFileExit = ff.FindNextFile();
CString tempSrcPath = ff.GetFilePath();
CString strTitle = ff.GetFileTitle();//得到目录名
CString tempDesPath = tempSrcPath;
tempDesPath.Replace(strSrcPath,strDesPath);
if(ff.IsDirectory() && !ff.IsDots())
{
CreateDirectory(strTitle,NULL);
copyFiles(tempSrcPath,tempDesPath);
}
else
if( !ff.IsDirectory() && !ff.IsDots())
{
CString tempSrcPath = ff.GetFilePath();
CopyFile(tempSrcPath,tempDesPath,false);
}
}
}
// 将pc中的文件夹从一个目录拷贝到另外的一个目录
BOOL CopyDirectory(CString strSrcPath, CString strDesPath, BOOL bFailIfExists/*=FALSE*/)
{
if( strSrcPath.IsEmpty() )
{
OutputDebugString("源文件名为空,无法进行拷贝!");
return FALSE;
}
if ( !DirectoryExist(strDesPath) )
{
if ( !PCNewFileCopy( strDesPath ) )
return FALSE;
}
if ( strSrcPath.GetAt(strSrcPath.GetLength()-1) != '\\' )
strSrcPath += '\\';
if ( strDesPath.GetAt(strDesPath.GetLength()-1) != '\\' )
strDesPath += '\\';
BOOL bRet = FALSE; // 因为源目录不可能为空,所以该值一定会被修改
CFileFind ff;
BOOL bFound = ff.FindFile(strSrcPath+"*", 0);
while(bFound) // 递归拷贝
{
bFound = ff.FindNextFile();
if( ff.GetFileName() == "." || ff.GetFileName() == ".." )
continue;
CString strSubSrcPath = ff.GetFilePath();
CString strSubDespath = strSubSrcPath;
strSubDespath.Replace(strSrcPath, strDesPath);
if( ff.IsDirectory() )
bRet = CopyDirectory(strSubSrcPath, strSubDespath, bFailIfExists); // 递归拷贝文件夹
else
bRet = CopyFile(strSubSrcPath, strSubDespath, bFailIfExists); // 拷贝文件
if ( !bRet )
break;
}
ff.Close();
return bRet;
}