http://www.51xuewen.com/blog/H_aShow.aspx?blog=huym&ID=420
自己用vc写的一个拷贝文件夹函数
2008-4-16 18:19:42 |
前两天,在项目中遇到一个需要从一个服务器上拷贝文件到另一台机子的问题。开始考虑使用SHFileOperation,在本机上试过感觉还不错,能考过去,但是这个函数把整个文件夹都copy过去了。当我想要从服务器上在根目录上建的共享目录里的文件拷出的时候,出问题了:这个共享目录是不能够被考出去的,而SHFileOperation只能拷贝文件夹下面的文件或子文件夹。想了很多方法,把SHFileOperation的参数、方法看了个遍也没能解决这个问题。没办法,只能另辟蹊径了,后来自己写了一个函数,虽然能够正常传输文件了,但是这个函数没有进度条,后来又自己加了一个进度条。 //复制设备文件到新目录下 BOOL CStoreDevice:: CopyFileAndFolder( LPSTR pszsrc,CString strztar) //pszsrc源路径,strztar目标路径 { CFileFind finder; CString strWildcard(pszsrc); CString strPathTar(strztar); CString strPathSrc(pszsrc); // start working for files strWildcard += _T("//*.*"); BOOL bWorking = finder.FindFile(strWildcard); while(bWorking) { bWorking = finder.FindNextFile(); strPathTar = strztar; if(!finder.IsDirectory()) { strPathTar.Insert(strPathTar.GetLength(), "//"); strPathTar.Insert(strPathTar.GetLength(), finder.GetFileName()); m_CopyFileDlg.StepIt(); if(!CopyFile((LPCSTR)finder.GetFilePath(), (LPCSTR)strPathTar, FALSE)) return FALSE; } else { if(finder.GetFileName().Compare(".") == 0 || finder.GetFileName().Compare("..") == 0 ) { continue; } strPathSrc = finder.GetFilePath(); strPathTar.Insert(strPathTar.GetLength(), "//"); strPathTar.Insert(strPathTar.GetLength(), finder.GetFileName()); CreateDirectory(strPathTar, NULL); CopyFileAndFolder((LPSTR)(LPCSTR)strPathSrc,strPathTar) ; } } return TRUE; } |
|
http://hi.baidu.com/jack_wu_hu/blog/item/d0ee791cc8b8088e87d6b69e.html
vc 递归拷贝文件夹
2008-06-13 15:29
bool Utility::CopyFolder(const CString &desc,const CString &source)
{
CString tmpFrom(source);
CString tmpTo(desc);
if (!PathFileExists(tmpTo))
{
CreateDirectory(tmpTo,NULL);
}
if (PathFileExists(tmpFrom))
{
WIN32_FIND_DATA FindFileData;
CString tmpFind(tmpFrom);
tmpFind += _T("//*.*");
HANDLE hFind=::FindFirstFile(tmpFind,&FindFileData);
if(INVALID_HANDLE_VALUE == hFind)
return false;
while(TRUE)
{
CString tmpFile(tmpTo);
tmpFile += _T("//");
tmpFile += FindFileData.cFileName;
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) //目录
{
if(FindFileData.cFileName[0]!='.')
{
if (!PathFileExists(tmpFile)) //目录不存在
{
CreateDirectory(tmpFile,NULL);
}
CopyFolder(tmpFile,tmpFrom + _T("//") + FindFileData.cFileName);
}
}
else //文件
{
if (!PathFileExists(tmpFile)) //文件不存在
{
CopyFile(tmpFrom + _T("//") + FindFileData.cFileName,tmpFile,TRUE); //拷贝文件
}
}
if(!FindNextFile(hFind,&FindFileData))
break;
}
FindClose(hFind);
}
return TRUE;
}
|