File copy, movement, deletion, and rename operations provided by the system

Source: Internet
Author: User

Preface it often takes about half an hour to manually copy the code of the system file. It uses a number of recursion operations to determine the file attributes and check whether it is a folder or a file. Then, you can control the progress of the progress bar based on the file size. Do you really need to pay so much?

Recently, I have studied windows shell programming and found that many of the functions of the system have been well developed and completely provided to us, but we prefer to maintain every process step by step. The final result is that the development progress is delayed and some unknown software bugs occur.


In versions earlier than vista, The SHFileOperation system function is often used to process operations similar to copying, moving, deleting, and renaming objects. However, in Versions later than vista, the system provides the com library interface IFileOperation to process the same file operations. Of course, the previous method still applies. However, the latest file operation method will be more user-friendly (will be mentioned later ).


Content
Copy old files
Int CEarlyFileOperator: FOCopyFile (const wstring & strFrom, const wstring & strTo) {wchar_t srcPath [MAX_PATH]; wchar_t dstPath [MAX_PATH]; memset (srcPath, '\ 0 ', MAX_PATH); memset (dstPath, '\ 0', MAX_PATH); memcpy (srcPath, strFrom. c_str (), strFrom. length () * sizeof (wchar_t); memcpy (dstPath, strTo. c_str (), strTo. length () * sizeof (wchar_t); SHFILEOPSTRUCT FileOp = {0}; FileOp. hwnd = NULL; // The Calling process changes the parent window attribute FileOp. wFunc = FO_COPY; // execute the file copy FileOp. pFrom = srcPath; FileOp. pTo = dstPath; FileOp. hNameMappings = NULL; FileOp. fFlags = FOF_ALLOWUNDO; FileOp. lpszProgressTitle = _ T ("the file is being copied... "); int nRet = SHFileOperation (& FileOp); return nRet ;}
Mobile Operations
Int CEarlyFileOperator: FORemoveFile (const wstring & strFrom, const wstring & strTo) {wchar_t srcPath [MAX_PATH]; wchar_t dstPath [MAX_PATH]; memset (srcPath, '\ 0 ', MAX_PATH); memset (dstPath, '\ 0', MAX_PATH); memcpy (srcPath, strFrom. c_str (), strFrom. length () * sizeof (wchar_t); memcpy (dstPath, strTo. c_str (), strTo. length () * sizeof (wchar_t); SHFILEOPSTRUCT FileOp = {0}; FileOp. hwnd = NULL; // The Calling process changes the parent window attribute FileOp. wFunc = FO_MOVE; // execute the file copy FileOp. pFrom = srcPath; FileOp. pTo = dstPath; FileOp. hNameMappings = NULL; FileOp. fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR; // FileOp. lpszProgressTitle = _ T ("the file is being copied... "); int nRet = SHFileOperation (& FileOp); return nRet ;}
Delete operation
Int CEarlyFileOperator: FODelFile (const wstring & strFrom) {wchar_t srcPath [MAX_PATH]; memset (srcPath, '\ 0', MAX_PATH); memcpy (srcPath, strFrom. c_str (), strFrom. length () * sizeof (wchar_t); SHFILEOPSTRUCT FileOp = {0}; FileOp. hwnd = NULL; FileOp. wFunc = FO_DELETE; FileOp. pFrom = srcPath; FileOp. pTo = NULL; FileOp. hNameMappings = NULL; FileOp. fFlags = FOF_ALLOWUNDO; // undo allowed. The confirmation dialog box int nRet = SHFileOperation (& FileOp); return nRet;} is not displayed ;}
RENAME operation
Int CEarlyFileOperator: FORenameFile (const wstring & strFrom, const wstring & strRename) {wchar_t srcPath [MAX_PATH]; wchar_t reName [MAX_PATH]; memset (srcPath, '\ 0 ', MAX_PATH); memset (reName, '\ 0', MAX_PATH); // obtain the path address wstring strPath = strFrom. substr (0, strFrom. rfind (_ T ("\"); wstring strTo = strPath + _ T ("\") + strRename; memcpy (srcPath, strFrom. c_str (), strFrom. length () * sizeof (wchar_t); memcpy (reName, strTo. c_str (), strTo. length () * sizeof (wchar_t); SHFILEOPSTRUCT FileOp = {0}; FileOp. hwnd = NULL; // The Calling process changes the parent window attribute FileOp. wFunc = FO_RENAME; // execute the file copy FileOp. pFrom = srcPath; FileOp. pTo = reName; FileOp. hNameMappings = NULL; FileOp. fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION; int nRet = SHFileOperation (& FileOp); return nRet ;}

New file copy operations
int CLaterFileOperator::FOCopyFile(const wstring &strFrom, const wstring &strTo){HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)){IFileOperation *pfo;///< Create the IFileOperation interfacehr = CoCreateInstance(CLSID_FileOperation, NULL, CLSCTX_ALL, IID_PPV_ARGS(&pfo));if (SUCCEEDED(hr)){///< Set the operation flagshr = pfo->SetOperationFlags(FOF_ALLOWUNDO);if (SUCCEEDED(hr)){///< Create an IShellItem from the supplied source pathIShellItem *psiFrom = NULL;hr = SHCreateItemFromParsingName(strFrom.c_str(), NULL, IID_PPV_ARGS(&psiFrom));if (SUCCEEDED(hr)){///< Create an IShellItem from the supplied destination path.IShellItem *psiTo = NULL;hr = SHCreateItemFromParsingName(strTo.c_str(), NULL, IID_PPV_ARGS(&psiTo));if (SUCCEEDED(hr)){hr = pfo->CopyItem(psiFrom, psiTo, NULL, NULL);if (NULL != psiTo){psiTo->Release();}}psiFrom->Release();}if (SUCCEEDED(hr)){hr = pfo->PerformOperations();} }pfo->Release();}CoUninitialize();}return hr;}
Mobile Operations
int CLaterFileOperator::FORemoveFile(const wstring &strFrom, const wstring &strTo){HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)){IFileOperation *pfo;///< Create the IFileOperation interfacehr = CoCreateInstance(CLSID_FileOperation, NULL, CLSCTX_ALL, IID_PPV_ARGS(&pfo));if (SUCCEEDED(hr)){///< Set the operation flagshr = pfo->SetOperationFlags(FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR);if (SUCCEEDED(hr)){///< Create an IShellItem from the supplied source pathIShellItem *psiFrom = NULL;hr = SHCreateItemFromParsingName(strFrom.c_str(), NULL, IID_PPV_ARGS(&psiFrom));if (SUCCEEDED(hr)){///< Create an IShellItem from the supplied destination path.IShellItem *psiTo = NULL;hr = SHCreateItemFromParsingName(strTo.c_str(), NULL, IID_PPV_ARGS(&psiTo));if (SUCCEEDED(hr)){hr = pfo->MoveItem(psiFrom, psiTo, NULL, NULL);if (NULL != psiTo){psiTo->Release();}}psiFrom->Release();}if (SUCCEEDED(hr)){hr = pfo->PerformOperations();} }pfo->Release();}CoUninitialize();}return hr;}
Delete operation
int CLaterFileOperator::FODelFile(const wstring &strFrom){HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)){IFileOperation *pfo;///< Create the IFileOperation interfacehr = CoCreateInstance(CLSID_FileOperation, NULL, CLSCTX_ALL, IID_PPV_ARGS(&pfo));if (SUCCEEDED(hr)){///< Set the operation flagshr = pfo->SetOperationFlags(FOF_ALLOWUNDO);if (SUCCEEDED(hr)){///< Create an IShellItem from the supplied source pathIShellItem *psiFrom = NULL;hr = SHCreateItemFromParsingName(strFrom.c_str(), NULL, IID_PPV_ARGS(&psiFrom));if (SUCCEEDED(hr)){hr = pfo->DeleteItem(psiFrom, NULL);psiFrom->Release();}if (SUCCEEDED(hr)){hr = pfo->PerformOperations();} }pfo->Release();}CoUninitialize();}return hr;}
RENAME operation
int CLaterFileOperator::FORenameFile(const wstring &strFrom, const wstring &strRename){HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)){IFileOperation *pfo;///< Create the IFileOperation interfacehr = CoCreateInstance(CLSID_FileOperation, NULL, CLSCTX_ALL, IID_PPV_ARGS(&pfo));if (SUCCEEDED(hr)){///< Set the operation flagshr = pfo->SetOperationFlags(FOF_ALLOWUNDO | FOF_NOCONFIRMATION);if (SUCCEEDED(hr)){///< Create an IShellItem from the supplied source pathIShellItem *psiFrom = NULL;hr = SHCreateItemFromParsingName(strFrom.c_str(), NULL, IID_PPV_ARGS(&psiFrom));if (SUCCEEDED(hr)){hr = pfo->RenameItem(psiFrom, strRename.c_str(), NULL);psiFrom->Release();}if (SUCCEEDED(hr)){hr = pfo->PerformOperations();} }pfo->Release();}CoUninitialize();}return hr;}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.