windows程序中拷貝文件的選擇
阿新 • • 發佈:2017-07-29
sizeof attr mkdir hfile 需要 keyword detail repl att
最近需要在Windows下拷貝大量小文件(數量在十萬級別以上)。寫了些拷貝文件的小程序,竟然發現不同的選擇,拷貝的速度有天壤之別!
現有這樣的測試數據:1500+小文件,總大小10M左右。現用不同方法進行拷貝。:
方案1:調用SHFileOperation
[cpp] view plain copy
- 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
[cpp] view plain copy
- BOOL CUtility::CopyFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)
- {
- return CopyFile(lpszFromPath, lpszToPath, TRUE);
- }
代碼短小精悍。復制用時:700毫秒。
方案3:調用CMD命令。
[cpp] view plain copy
- 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程序中拷貝文件的選擇