在vc中使用何api获类将一个目录下的所有文件拷贝到其他目录下,谢谢

解决方案 »

  1.   

    WinExec("xcpoy dir\*.* otherdir\", SW_HIDE);
      

  2.   

    应该没有这样的函数!你需要可以写,特别是需要进度条的情况!以下是我自己用的一个函数,你可以参考: static BOOL CopyFileEx(CString szExistingFileName,CString szNewFileName,bool bRemoveOrgFile,CProgressListener* pListener)
    {
    //Is it a folder?
    if(IsDirectoryExist(szExistingFileName))
    {
    //Create the target folder
    RCreateDirectory(szNewFileName); //creat target folder success?
    if(IsDirectoryExist(szNewFileName))
    {
    //Get all sub folder or files
    CFileFind Finder;
    BOOL bFound=Finder.FindFile(szExistingFileName+_T("\\*.*"));
    while(bFound)
    {
    bFound=Finder.FindNextFile();
    if(Finder.IsDots()) //not preccess "." or ".."
    continue;
    CString szChildName=Finder.GetFileName();
    CString szExistSubName=szExistingFileName+_T("\\")+szChildName;
    CString szNewSubName=szNewFileName+_T("\\")+szChildName; //recursive call the function 
    if(!CopyFileEx(szExistSubName,szNewSubName,bRemoveOrgFile,pListener))
    return false; //Copy fail //delete old folder
    if(bRemoveOrgFile&&IsDirectoryExist(szExistSubName))
    if(!RemoveDirectory(szExistSubName))
    if(pListener!=NULL)
    pListener->ActionFail(MA_DELETE,_T(""),szExistSubName); //Don't care the result!
    } //end of get sub folders or files

    return true; //folder proccess finish
    }
    else //folder szNewFileName creat fail
    return false;
    }
    else //szExistingFileName is not a folder
    {
    BOOL bContinue=true;
    MediaAction nAction=bRemoveOrgFile?MA_MOVE:MA_COPY;
    //Check the existen of target file
    if(IsFileExist(szNewFileName))
    {
    //should we overwrite the exist file?
    if(pListener)
    if(!pListener->OverwritePrompt(szNewFileName))
    return true; //Not overwrite ,and continue with next file!
    } //Create caller object 
    CListenerCaller* pCaller=NULL;
    if(pListener)
    {
    pCaller=new CListenerCaller;
    pCaller->m_nAction=nAction;
    pCaller->m_szSource=szExistingFileName;
    pCaller->m_szTarget=szNewFileName;
    pCaller->m_pListener=pListener;
    } //Start copy file
    BOOL result=::CopyFileEx(szExistingFileName,szNewFileName,CopyProgressRoutine,(void*)pCaller,NULL,NULL); //Save last error code
    DWORD dwError=GetLastError();
    if(!result) //Copy fail
    {
    //call the listener
    if(pListener)
    pListener->ActionFail(nAction,szExistingFileName,szNewFileName); //Don't care the result! //don't continue any more
    bContinue=false;
    }
    else //Copy success
    {
    //Set file attribute to normal
    ::SetFileAttributes(szNewFileName,FILE_ATTRIBUTE_NORMAL); //delete old file
    if(bRemoveOrgFile)
    if(!DeleteFile(szExistingFileName))
    if(pListener!=NULL)
    pListener->ActionFail(MA_DELETE,_T(""),szExistingFileName); //Don't care the result!
    } if(pCaller)
    delete pCaller; //Restore the error code
    SetLastError(dwError); //should we continue?
    return bContinue;
    }
    }