现在的位置: 首页 > 综合 > 正文

VC复制文件夹并复制文件夹内所有文件

2018年02月08日 ⁄ 综合 ⁄ 共 1727字 ⁄ 字号 评论关闭
复制文件两个方法:
1 套用mfc原有函数
2 采用递归方式

3 采用system();    //调用终端命令复制,缺点很明显,忽略

// 文件路径   偏移量   修改的数据  输入的修改数据的长度
bool CImageClass::FseekToChange(CString filePath, int seek, char * buf, int bufLength)
{
 CFile file;
 if(file.Open(filePath,CFile::modeReadWrite))
 {
  file.Seek(seek,CFile::begin);
  file.Write(buf,bufLength);
  file.Close();
  return true;
 }
 else
  return false;
}
// 拷贝原文件夹内所有文件到目标文件夹内   源文件夹   目标文件夹
void CImageClass::CopyDirectory(LPCSTR SrcFilePath, LPCSTR DestFilePath)
{
 SHFILEOPSTRUCT   sfo;
 sfo.hwnd =   NULL;
 sfo.wFunc =   FO_COPY;
 sfo.pFrom =   SrcFilePath;  
 sfo.pTo  =   DestFilePath;
 sfo.fFlags =  FOF_NOCONFIRMMKDIR ;
 if(SHFileOperationA(&sfo)==0)
  return ;
}
void CImageClass::CopyDirectory(CString SrcFilePath, CString DestFilePath)
{
 CString strFinder=SrcFilePath+"\\*";
 CString SrcTemp;
 CString fileName;
 CFileFind finder;
 LPCSTR Src;
 LPCSTR Dest=(LPCSTR)DestFilePath;
 if(!finder.FindFile(strFinder)) return;
 while(finder.FindNextFileA())
 {
  if(!finder.IsDots())   //不是目录
  {
   SrcTemp=finder.GetFileName();
   fileName=SrcFilePath+"\\"+SrcTemp;
   Src=(LPCSTR)fileName;
   CopyDirectory(Src,Dest);
   fileName.Empty();
   SrcTemp.Empty();
  }
 }
}


方式2
/*文件夹内容拷贝*/
void CImageClass::CopyDirectory(CString SrcFilePath, CString DestFilePath)
{
 /*judge if dpath exist, if not then create*/
 CFileFind dfinder;
 if (!dfinder.FindFile((LPCTSTR)DestFilePath))
 {
  CreateDirectory(DestFilePath,NULL);
 }
 dfinder.Close();
 
 /*copy all the file and subdir to dpath*/
 CString strTarget = SrcFilePath + "\\*.*";
 
 CFileFind finder;
 BOOL bWorking = finder.FindFile((LPCTSTR)strTarget);
 while (bWorking)
 {
  bWorking = finder.FindNextFile();  
  CString strFileName  = finder.GetFileName();
  CString strNewSPath = SrcFilePath + "\\" + strFileName;
  CString strnewDPath = DestFilePath + "\\" + strFileName;
  /*if sub dir*/
  if ( !finder.IsDots() && finder.IsDirectory() )
  {
      CopyDirectory(strNewSPath, strnewDPath);
  }
  else /*if file*/
  {
   CopyFile(strNewSPath, strnewDPath, FALSE);
  }
 
 }
 finder.Close();
} 

抱歉!评论已关闭.