C++学习之:复制和粘贴文件/文件夹

来源:互联网 发布:sql 多表查询结果合并 编辑:程序博客网 时间:2024/05/17 11:04

从剪贴板上获得要复制/剪贴的文件路径之后,如何完成文件/文件夹的粘贴呢。下面列出了两个函数用来完成这些功能

  1. //复制的粘贴
  2. DWORD VrvCopyFile( LPCWSTR lpSrc, LPCWSTR lpDst ) 
  3.     DWORD   dwError         =   0; 
  4.     CString strFiles = lpSrc; 
  5.     CString strFilePath = _T(""); 
  6.     int idx= 0; 
  7.     while (strFiles.Find('#',idx) != -1) 
  8.     { 
  9.         int y = strFiles.Find('#',idx); 
  10.         strFilePath = strFiles.Mid(idx,y - idx); 
  11.         TCHAR szFile[MAX_PATH] = {0}; 
  12.         lstrcpy(szFile,strFilePath.GetBuffer()); 
  13.         strFilePath.ReleaseBuffer(); 
  14.         idx = strFiles.Find('#',idx); 
  15.         idx++; 
  16.         SHFILEOPSTRUCT shFileOp = {0}; 
  17.         shFileOp.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR; 
  18.         shFileOp.pFrom = szFile; 
  19.         shFileOp.pTo = lpDst; 
  20.         shFileOp.wFunc = FO_COPY; 
  21.         dwError = SHFileOperation(&shFileOp); 
  22.     } 
  23.      
  24.     return dwError; 
  25.  
  26. //剪贴的粘贴
  27. DWORD VrvMoveFile(LPCWSTR lpSrc, LPCWSTR lpDst) 
  28.     DWORD   dwError         =   0; 
  29.     CString strFiles = lpSrc; 
  30.     CString strFilePath = _T(""); 
  31.     int idx= 0; 
  32.     while (strFiles.Find('#',idx) != -1) 
  33.     { 
  34.         int y = strFiles.Find('#',idx); 
  35.         strFilePath = strFiles.Mid(idx,y - idx); 
  36.         TCHAR szFile[MAX_PATH] = {0}; 
  37.         lstrcpy(szFile,strFilePath.GetBuffer()); 
  38.         strFilePath.ReleaseBuffer(); 
  39.         idx = strFiles.Find('#',idx); 
  40.         idx++; 
  41.         SHFILEOPSTRUCT shFileOp = {0}; 
  42.         shFileOp.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR; 
  43.         shFileOp.pFrom = szFile; 
  44.         shFileOp.pTo = lpDst; 
  45.         shFileOp.wFunc = FO_MOVE; 
  46.         dwError = SHFileOperation(&shFileOp); 
  47.     } 
  48.  
  49.     return dwError; 

源路径存放了多个文件路径,使用#分开,每个文件/文件夹逐个粘贴。

本文出自 “麻雀的家” 博客,请务必保留此出处http://3457302.blog.51cto.com/3447302/902593