最近需要在Windows下拷贝大量小文件(数量在十万级别以上)。写了些拷贝文件的小程序,竟然发现不同的选择,拷贝的速度有天壤之别!
现有这样的测试数据:1500+小文件,总大小10M左右。现用不同方法进行拷贝。:
方案1:调用SHFileOperation
- BOOL CUtility::CopyFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)
- {
- size_t nLengthFrm = _tcslen(lpszFromPath);
- TCHAR *NewPathFrm = new TCHAR[nLengthFrm+2];
- _tcscpy(NewPathFrm,lpszFromPath);
- NewPathFrm[nLengthFrm] = ‘\0‘;
- NewPathFrm[nLengthFrm+1] = ‘\0‘;
-
- SHFILEOPSTRUCT FileOp;
- ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));
- FileOp.fFlags = FOF_NOCONFIRMATION|FOF_NOCONFIRMMKDIR|FOF_NOERRORUI|FOF_FILESONLY|FOF_NOCOPYSECURITYATTRIBS ;
- FileOp.hNameMappings = NULL;
- FileOp.hwnd = NULL;
- FileOp.lpszProgressTitle = NULL;
- FileOp.pFrom = NewPathFrm;
- FileOp.pTo = lpszToPath;
- FileOp.wFunc = FO_COPY;
- return return SHFileOperation(&FileOp);
- }
代码比较罗索。复制完成用时:57,923毫秒。
方案2:调用API:CopyFile
- BOOL CUtility::CopyFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)
- {
- return CopyFile(lpszFromPath, lpszToPath, TRUE);
- }
代码短小精悍。复制用时:700毫秒。
方案3:调用CMD命令。
- BOOL CUtility::CopyFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)
- {
- TCHAR tbuff[255];
- char buff[255];
- _stprintf(tbuff, _T("copy /Y %s %s"), lpszFromPath, lpszToPath);
- TChar2Char(tbuff, buff, 255);
- system(buff);
- return TRUE;
- }
跑到5分钟后直接卡死。。没有得出结果,可能是参数传递的问题。
http://blog.csdn.net/lsldd/article/details/8191338
windows程序中拷贝文件的选择
原文:http://www.cnblogs.com/findumars/p/7252818.html