void string_replace(string & strBig, const string & strsrc, const string &strdst)
{
string::size_type pos=0;
string::size_type srclen=strsrc.size();
string::size_type dstlen=strdst.size();
while( (pos=strBig.find(strsrc, pos)) != string::npos)
{
strBig.erase(pos, srclen);
strBig.insert(pos, strdst);
pos += dstlen;
}
}
void PathConvert(string& strAbsPath, string strRelativePath)
{
int nFind = strRelativePath.find("..");
if ( nFind < 0 )
{
strAbsPath = strRelativePath;
return;
}
string str1 = strRelativePath.substr(0, nFind);
string str2 = strRelativePath.substr(nFind, strRelativePath.length() - nFind);
TCHAR szAbsPath[MAX_PATH] = {0};
PathCombine(szAbsPath, str1.c_str(), str2.c_str());
strAbsPath = szAbsPath;
}
void PrepareCopyFolders(vector<string>& listFolders, string strSrcFolder,
vector<string> listNotCopyFolders)
{
TCHAR szFindForder[MAX_PATH] = {0};
wsprintf(szFindForder, TEXT("%s\\*.*"), strSrcFolder.c_str());
WIN32_FIND_DATA wfd;
HANDLE hFileFind = FindFirstFile(szFindForder, &wfd);
if ( hFileFind == INVALID_HANDLE_VALUE )
return;
BOOL bOK = TRUE;
while ( bOK )
{
if ( lstrcmpi(wfd.cFileName, TEXT(".")) == 0 ||
lstrcmpi(wfd.cFileName, TEXT("..")) == 0 )
{
bOK = FindNextFile(hFileFind, &wfd);
continue;
}
BOOL bCopy = TRUE;
int nCount = listNotCopyFolders.size();
for ( int i = 0; i < nCount; i++ )
{
if ( lstrcmpi(wfd.cFileName, listNotCopyFolders[i].c_str()) == 0 )
{
bCopy = FALSE;
break;
}
}
if ( !bCopy )
{
bOK = FindNextFile(hFileFind, &wfd);
continue;
}
string strFindFoler = strSrcFolder + string("\\") + string(wfd.cFileName);
if ( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
listFolders.push_back(strFindFoler);
PrepareCopyFolders(listFolders, strFindFoler, listNotCopyFolders);
}
bOK = FindNextFile(hFileFind, &wfd);
}
}
bool FilterCopy(string strSrcFolder, string strDstFolder,
vector<string> listNotCopyFolders,
vector<string> listNotCopyFilesExt,
vector<string> listNotCopyFiles)
{
int nFind = strSrcFolder.find(':');
if ( nFind <= 0 )
return false;
nFind = strDstFolder.find(':');
if ( nFind <= 0 )
return false;
strDstFolder += string("\\");
PathConvert(strSrcFolder, strSrcFolder);
PathConvert(strDstFolder, strDstFolder);
nFind = strDstFolder.find('\\', 0);
while ( nFind > 0 )
{
nFind = strDstFolder.find('\\', nFind + 1);
if ( nFind < 0 )
break;
string strSubFolder = strDstFolder.substr(0, nFind);
CreateDirectory(strSubFolder.c_str(), NULL);
}
vector<string> listSrcFolders;
listSrcFolders.push_back(strSrcFolder);
PrepareCopyFolders(listSrcFolders, strSrcFolder, listNotCopyFolders);
int i;
int nCount = listSrcFolders.size();
for ( i = 0; i < nCount; i++ )
{
string str = listSrcFolders[i];
string_replace(str, strSrcFolder, strDstFolder);
CreateDirectory(str.c_str(), NULL);
TCHAR szFindFolder[MAX_PATH] = {0};
wsprintf(szFindFolder, TEXT("%s\\*.*"), listSrcFolders[i].c_str());
WIN32_FIND_DATA wfd;
HANDLE hFileFind = FindFirstFile(szFindFolder, &wfd);
if ( INVALID_HANDLE_VALUE == hFileFind )
continue;
BOOL bOK = TRUE;
while ( bOK )
{
if ( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
bOK = FindNextFile(hFileFind, &wfd);
continue;
}
BOOL bCopy = TRUE;
int nNotCopyCount = listNotCopyFiles.size();
for ( int j = 0; j < nNotCopyCount; j++ )
{
if ( lstrcmpi(wfd.cFileName, listNotCopyFiles[j].c_str()) == 0 )
{
bCopy = FALSE;
break;
}
}
if ( !bCopy )
{
bOK = FindNextFile(hFileFind, &wfd);
continue;
}
bCopy = TRUE;
nNotCopyCount = listNotCopyFilesExt.size();
for ( int k = 0; k < nNotCopyCount; k++ )
{
string strFile = string(wfd.cFileName);
nFind = strFile.rfind('.');
if ( nFind < 0 )
continue;
strFile = strFile.substr(nFind, strFile.length() - nFind);
if ( lstrcmpi(strFile.c_str(), listNotCopyFilesExt[k].c_str()) == 0 )
{
bCopy = FALSE;
break;
}
}
if ( !bCopy )
{
bOK = FindNextFile(hFileFind, &wfd);
continue;
}
TCHAR szSrcFile[MAX_PATH] = {0};
TCHAR szDstFile[MAX_PATH] = {0};
wsprintf(szSrcFile, TEXT("%s\\%s"), listSrcFolders[i].c_str(), wfd.cFileName);
wsprintf(szDstFile, TEXT("%s\\%s"), str.c_str(), wfd.cFileName);
if ( !CopyFile(szSrcFile, szDstFile, FALSE) )
return false;
bOK = FindNextFile(hFileFind, &wfd);
}
}
return true;
}
|