在WinCE下實作将某檔案夾下的所有檔案(包括檔案夾)拷貝到另一個檔案夾中.
算法不複雜,簡單實用.
//szExistingDir:源檔案夾
//szNewDir:目标檔案夾
//注意:目标檔案夾必須要存在,否則該函數将傳回FALSE.
BOOL BrowseAndCopy(const CString szExistingDir, const CString szNewDir)
{
CString szExistDir;
CString szAimDir=szNewDir; //儲存目标檔案夾路徑
CString szFindDir=szExistingDir; //儲存源檔案夾路徑
if(szFindDir.Right(1)!="//")
{
szFindDir+="//";
szExistDir=szFindDir;
}
szFindDir+="*.*"; //搜尋所有檔案
WIN32_FIND_DATA fd;
HANDLE hFind;
hFind=FindFirstFile(szFindDir,&fd); //尋找第一個檔案
if(hFind!=INVALID_HANDLE_VALUE)
{
do{
if(fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) //判斷是否檔案夾
{
if(szAimDir.Right(1)!="//")
{
szAimDir+="//";
}
CreateDirectory(szAimDir+fd.cFileName,NULL); //在目标檔案夾中建立相應的子檔案夾
BrowseAndCopy(szExistDir+fd.cFileName,szAimDir+fd.cFileName); //采用遞歸查找子檔案下的檔案
}
else
{
if(szAimDir.Right(1)!="//")
{
szAimDir+="//";
}
if(CopyFile(szExistDir+fd.cFileName,szAimDir+fd.cFileName,FALSE)==FALSE) //拷貝檔案到目标檔案夾
{
return FALSE;
}
}
}while(FindNextFile(hFind,&fd)); //查找是否存在下一個檔案
}
else
{
//源檔案夾為空,傳回
return FALSE;
} return TRUE;
}