1. 程式人生 > >windows程序中拷貝文件的選擇

windows程序中拷貝文件的選擇

sizeof attr mkdir hfile 需要 keyword detail repl att

最近需要在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] = ‘\0‘;
  7. NewPathFrm[nLengthFrm+1] = ‘\0‘;
  8. SHFILEOPSTRUCT FileOp;
  9. ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));
  10. FileOp.fFlags = FOF_NOCONFIRMATION|FOF_NOCONFIRMMKDIR|FOF_NOERRORUI|FOF_FILESONLY|FOF_NOCOPYSECURITYATTRIBS ;
  11. FileOp.hNameMappings = NULL;
  12. FileOp.hwnd = NULL;
  13. FileOp.lpszProgressTitle = NULL;
  14. FileOp.pFrom = NewPathFrm;
  15. FileOp.pTo = lpszToPath;
  16. FileOp.wFunc = FO_COPY;
  17. return return SHFileOperation(&FileOp);
  18. }


代碼比較羅索。復制完成用時: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

windows程序中拷貝文件的選擇