在热心的网友帮助下,程序代码出来了。但是不能成功运行。帮忙调试一下,能运行但是不能完成任务。帮忙看看!程序的目的是把不同文件夹下的文件拷贝到指定的文件夹!void CMy11Dlg::OnOK() 
{
// TODO: Add extra validation here
TCHAR pszPath[MAX_PATH];  
BROWSEINFO bi;   
bi.hwndOwner      = this->GetSafeHwnd();  
bi.pidlRoot       = NULL;  
bi.pszDisplayName = NULL;   
bi.lpszTitle      = _T("请选择文件夹");   
bi.ulFlags        = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;  
bi.lpfn           = NULL;   
bi.lParam         = 0;  
bi.iImage         = 0;   
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);  
if (pidl == NULL)  
{  
return;  

CString strDst = "D:\\data\\";
CreateDirectory(strDst, NULL);
if (SHGetPathFromIDList(pidl, pszPath))  
{  
CFileFind finder; 
CString strSrc;
strSrc.Format("%s\\*.xls",pszPath);//如果是所有文件将.xls改为*.*
BOOL bFind = finder.FindFile(strSrc); 
while (bFind) 

bFind = finder.FindNextFile(); 
if (finder.IsDots()) 

continue; 

if (finder.IsDirectory())
{
continue;//如果pszPath中有目录,并目录中还有.xml则可递归find和copy
}
else

                //拷贝文件
CString strDstFile = strDst + finder.GetFileName();//这里可以直接通过CString的"CString::operator +"来将两个字符串组合
if(!CopyFile(strSrc, strDstFile, FALSE))

{
MessageBox("拷贝成功");

}




finder.Close(); 

CDialog::OnOK();
}
mfc文件复制

解决方案 »

  1.   

    不管你是createdirectory或是copyfile,首选确保目标的父目录存在createdirectory(d:\a\b) d:\a必须存在
    copyfile(d:\a\b.txt, e:\a\b.txt); e:\a必须存在
      

  2.   


    bool Normalize( CString&path )
    {
    bool ret = !path.IsEmpty();
    if (ret)
    {
    wchar_t slashes = path[path.GetLength()-1];
    if (slashes != L'\\' 
    && slashes != L'/')
    {
    if (path.Find(L'/') != -1){
    path += L"/";
    }
    else{
    path += L"\\";
    }
    }
    path.Replace(L'/', L'\\');
    path.Replace(L"\\\\", L"\\");
    }
    return ret;
    } void CopyFolderAllFiles(CString  csSourceFolder,   CString  csNewFolder, bool &result) 

    CStringsrc = csSourceFolder;
    CStringdest = csNewFolder;
    Normalize(src);
    Normalize(dest);
    CFileFindEx   f; 
    BOOL   bFind=f.FindFile(src+ L"*.*"); 
    while(bFind) 

    bFind   =   f.FindNextFile(); 

    if(f.IsDots())   
    continue;  CStringname = f.GetFileName();
    if(f.IsDirectory()) 

    if(!::CreateDirectory(dest+name, NULL)){
    result = false;
    break;
    }
    else{
    CopyFolderAllFiles(src+name,dest+name, result); 
    }

    else
    {
    ::SetFileAttributes(src+name,FILE_ATTRIBUTE_NORMAL); 
    if(!::CopyFile(src+name,dest+ name,FALSE))
    {
    result = false;
    break;
    }
    }

    }