MFC 目录选取,指定类型文件查找复制

目录选取:
 BROWSEINFO bi;
 ZeroMemory(&bi, sizeof(BROWSEINFO));
 bi.hwndOwner = m_hWnd;
 bi.ulFlags   = BIF_RETURNONLYFSDIRS;
 bi.lpszTitle = _T("Browse A Input Path");
 LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
 BOOL bRet = FALSE;
 TCHAR szFolder[MAX_PATH*2];
 szFolder[0] = _T('/0');
 if (pidl)
 {
  if (SHGetPathFromIDList(pidl, szFolder)) 
   bRet = TRUE;
  IMalloc *pMalloc = NULL;
  if (SUCCEEDED(SHGetMalloc(&pMalloc)) && pMalloc)
  {
   pMalloc->Free(pidl);
   pMalloc->Release();
  }
 }
 if(bRet)
 {
  m_strInputPath = szFolder;//选择的文件夹路径
  SetDlgItemText(IDC_EINPUT,m_strInputPath);
 }
 
/
递归查找指定类型的文件:
void CFileFilterToolDlg::Recurse(CString strDir,CString strExt)
{  
 CFileFind   finder;
 CString   strCurrDir;  
 strCurrDir = strDir + _T("\\*.*");  
 BOOL bWorking = finder.FindFile(strCurrDir);  
 while(bWorking)  
 {  
  bWorking   =   finder.FindNextFile();    
  CString tempFileName = finder.GetFilePath();
  //cout<< (LPCTSTR) temp<<endl;
  if(finder.IsDots())continue;  
  if(finder.IsDirectory())  
  {  
   CString   str   =   finder.GetFilePath();  
   //   cout   <<   (LPCTSTR)   str   <<   endl;
   Recurse(str,strExt);  
  }
  else
  {
   //MessageBox(tempFileName);
   CString strCurrFileExt = tempFileName.Right(tempFileName.GetLength() - tempFileName.ReverseFind(_T('.')));
   //MessageBox(strCurrFileExt);
   CString strCurrFileName = tempFileName.Right(tempFileName.GetLength() - tempFileName.ReverseFind(_T('\\')) - 1);
   //MessageBox(strCurrFileName);
   CString strDstFileName = m_strOutputPath + _T("\\") + strCurrFileName;
   //MessageBox(strDstFileName);
   if(0 == strCurrFileExt.Compare(strExt))
   {
    int nRet = MCopyFile(tempFileName,strDstFileName,this->m_bCoverWrite);
    CString strMsg = strCurrFileName;
    if(0 == nRet)
    {
     m_nCountFindFilsCopySuccess++;
     strMsg += _T(" :Success");
    }
    else if(-1 == nRet)
    {
     CString str;
     str.Format(_T("复制文件%s时失败,该文件已存在!"),tempFileName);
     //MessageBox(str);
     strMsg += _T(" :") + str;
    }
    m_ctrlListFiles.InsertString(m_nCountTotalFilesFinded,strMsg);
    m_ctrlListFiles.SetCurSel(m_nCountTotalFilesFinded);
    m_nCountTotalFilesFinded++;
   }
   //MessageBox(strCurrFileExt);
  }
 }  
 finder.Close();  

/
//复制该文件
PathFileExists的头文件和库:
#include <shlwapi.h>
#pragma comment(lib,"Shlwapi.lib")
 
int MCopyFile(CString strSrc, CString strDest, BOOL cover)
{
 if(!cover)
 {
  if(PathFileExists(strDest))return -1;
 }
 //CopyFile((LPCSTR)strSrc, (LPCSTR)strDest, cover);
 CopyFile(strSrc, strDest, cover);
 return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值