windows程序中拷贝文件的选择

最近需要在Windows下拷贝大量小文件(数量在十万级别以上)。写了些拷贝文件的小程序,竟然发现不同的选择,拷贝的速度有天壤之别!

现有这样的测试数据:1500+小文件,总大小10M左右。现用不同方法进行拷贝。:

方案1:调用SHFileOperation

[cpp] view plain copy
 
  1. BOOL CUtility::CopyFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)  
  2. {  
  3.     size_t nLengthFrm = _tcslen(lpszFromPath);  
  4.     TCHAR *NewPathFrm = new TCHAR[nLengthFrm+2];  
  5.     _tcscpy(NewPathFrm,lpszFromPath);  
  6.     NewPathFrm[nLengthFrm] = '';  
  7.     NewPathFrm[nLengthFrm+1] = '';  
  8.   
  9.     SHFILEOPSTRUCT FileOp;  
  10.     ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));  
  11.     FileOp.fFlags = FOF_NOCONFIRMATION|FOF_NOCONFIRMMKDIR|FOF_NOERRORUI|FOF_FILESONLY|FOF_NOCOPYSECURITYATTRIBS ;  
  12.     FileOp.hNameMappings = NULL;  
  13.     FileOp.hwnd = NULL;  
  14.     FileOp.lpszProgressTitle = NULL;  
  15.     FileOp.pFrom = NewPathFrm;  
  16.     FileOp.pTo = lpszToPath;  
  17.     FileOp.wFunc = FO_COPY;  
  18.     return return SHFileOperation(&FileOp);  
  19. }  


代码比较罗索。复制完成用时:57,923毫秒。

方案2:调用API:CopyFile

[cpp] view plain copy
 
  1. BOOL CUtility::CopyFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)  
  2. {  
  3.     return CopyFile(lpszFromPath, lpszToPath, TRUE);  
  4. }  

代码短小精悍。复制用时:700毫秒。

方案3:调用CMD命令。

[cpp] view plain copy
 
  1. BOOL CUtility::CopyFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)  
  2. {  
  3.     TCHAR tbuff[255];  
  4.     char  buff[255];  
  5.     _stprintf(tbuff, _T("copy /Y %s %s"), lpszFromPath, lpszToPath);  
  6.     TChar2Char(tbuff, buff, 255);  
  7.     system(buff);  
  8.     return TRUE;  
  9. }  

跑到5分钟后直接卡死。。没有得出结果,可能是参数传递的问题。

http://blog.csdn.net/lsldd/article/details/8191338

原文地址:https://www.cnblogs.com/findumars/p/7252818.html