设为首页收藏本站

SKY外语、计算机论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 4827|回复: 0

[MFC] 复制文件夹.移动文件夹.删除文件夹(查找文件) 的实现(Win32)

[复制链接]

7

主题

0

好友

174

积分

注册会员

Rank: 2

生肖
星座
天秤座
性别
发表于 2012-6-12 18:48:45 |显示全部楼层
本帖最后由 sky_yx 于 2015-12-30 14:18 编辑

移动文件夹同分区下直接 MoveFile 即可
  1. #include <Windows.h>
  2. BOOL CopyDirectory(LPCTSTR szSourcePath,LPCTSTR szDestPath)
  3. {
  4.         //修正查找字符串
  5.         DWORD dwPathLen = _tcslen(szSourcePath);
  6.         LPTSTR szFindPath = new TCHAR[dwPathLen + 5];
  7.         _tcscpy_s(szFindPath,dwPathLen + 5,szSourcePath);
  8.         if(szFindPath[dwPathLen - 1] != _T('\\'))
  9.                 _tcscat_s(szFindPath,dwPathLen + 5,_T("\\"));
  10.         _tcscat_s(szFindPath,dwPathLen + 5,_T("*.*"));
  11.         CreateDirectory(szDestPath,NULL);
  12.         WIN32_FIND_DATA FindData;
  13.         HANDLE hFindFile = FindFirstFile(szFindPath,&FindData);
  14.         delete szFindPath;
  15.         if(hFindFile == INVALID_HANDLE_VALUE)return FALSE;
  16.         BOOL bIsSucceeded;
  17.         while(FindNextFile(hFindFile,&FindData))
  18.         {
  19.                 if(FindData.cFileName[0] == _T('.'))continue;
  20.                 //生成路径字符串
  21.                 DWORD dwFileLen = _tcslen(FindData.cFileName);
  22.                 LPTSTR szNewSrcPath = new TCHAR[dwPathLen + dwFileLen + 2];
  23.                 _tcscpy_s(szNewSrcPath,dwPathLen + dwFileLen + 2,szSourcePath);
  24.                 if(szNewSrcPath[dwPathLen - 1] != _T('\\'))
  25.                         _tcscat_s(szNewSrcPath,dwPathLen + dwFileLen + 2,_T("\\"));
  26.                 _tcscat_s(szNewSrcPath,dwPathLen + dwFileLen + 2,FindData.cFileName);
  27.                 LPTSTR szNewDstPath = new TCHAR[dwPathLen + dwFileLen + 2];
  28.                 _tcscpy_s(szNewDstPath,dwPathLen + dwFileLen + 2,szDestPath);
  29.                 if(szNewDstPath[dwPathLen - 1] != _T('\\'))
  30.                         _tcscat_s(szNewDstPath,dwPathLen + dwFileLen + 2,_T("\\"));
  31.                 _tcscat_s(szNewDstPath,dwPathLen + dwFileLen + 2,FindData.cFileName);
  32.                 if(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  33.                 {
  34.                         bIsSucceeded = CopyDirectory(szNewSrcPath,szNewDstPath);
  35.                 }
  36.                 else
  37.                 {
  38.                         bIsSucceeded = CopyFile(szNewSrcPath,szNewDstPath,FALSE);
  39.                 }
  40.                 delete szNewSrcPath;
  41.                 delete szNewDstPath;
  42.                 if(!bIsSucceeded)
  43.                 {
  44.                         FindClose(hFindFile);
  45.                         return FALSE;
  46.                 }
  47.         }
  48.         FindClose(hFindFile);
  49.         return bIsSucceeded;
  50. }
  51. BOOL DeleteDirectory(LPCTSTR szPath)
  52. {
  53.         //修正查找字符串
  54.         DWORD dwPathLen = _tcslen(szPath);
  55.         LPTSTR szDeletePath = new TCHAR[dwPathLen + 5];
  56.         _tcscpy_s(szDeletePath,dwPathLen + 5,szPath);
  57.         if(szDeletePath[dwPathLen - 1] != _T('\\'))
  58.                 _tcscat_s(szDeletePath,dwPathLen + 5,_T("\\"));
  59.         _tcscat_s(szDeletePath,dwPathLen + 5,_T("*.*"));
  60.         WIN32_FIND_DATA FindData;
  61.         HANDLE hFindFile = FindFirstFile(szDeletePath,&FindData);
  62.         delete szDeletePath;
  63.         if(hFindFile == INVALID_HANDLE_VALUE)return FALSE;
  64.         BOOL bIsSucceeded;
  65.         while(FindNextFile(hFindFile,&FindData))
  66.         {
  67.                 if(FindData.cFileName[0] == _T('.'))continue;
  68.                 //生成路径字符串
  69.                 DWORD dwFileLen = _tcslen(FindData.cFileName);
  70.                 szDeletePath = new TCHAR[dwPathLen + dwFileLen + 2];
  71.                 _tcscpy_s(szDeletePath,dwPathLen + dwFileLen + 2,szPath);
  72.                 if(szDeletePath[dwPathLen - 1] != _T('\\'))
  73.                         _tcscat_s(szDeletePath,dwPathLen + dwFileLen + 2,_T("\\"));
  74.                 _tcscat_s(szDeletePath,dwPathLen + dwFileLen + 2,FindData.cFileName);
  75.                 if(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  76.                 {
  77.                         bIsSucceeded = DeleteDirectory(szDeletePath);
  78.                 }
  79.                 else
  80.                 {
  81.                         bIsSucceeded = DeleteFile(szDeletePath);
  82.                 }
  83.                 delete szDeletePath;
  84.                 if(!bIsSucceeded)
  85.                 {
  86.                         FindClose(hFindFile);
  87.                         return FALSE;
  88.                 }
  89.         }
  90.         FindClose(hFindFile);
  91.         //生成要删除的文件夹路径字符串
  92.         szDeletePath = new TCHAR[dwPathLen + 2];
  93.         _tcscpy_s(szDeletePath,dwPathLen + 2,szPath);
  94.         if(szDeletePath[dwPathLen - 1] == _T('\\'))
  95.                 szDeletePath[dwPathLen - 1] = _T('\0');
  96.         //删除文件夹
  97.         bIsSucceeded = RemoveDirectory(szDeletePath);
  98.         delete szDeletePath;
  99.         return bIsSucceeded;
  100. }
  101. BOOL MoveDirectory(LPCTSTR szSourcePath,LPCTSTR szDestPath)
  102. {
  103.         return CopyDirectory(szSourcePath,szDestPath) && DeleteDirectory(szSourcePath);
  104. }
复制代码


+q67824885
您需要登录后才可以回帖 登录 | 立即注册


手机版|SKY外语计算机学习 ( 粤ICP备12031577 )    

GMT+8, 2024-3-28 22:56 , Processed in 0.131637 second(s), 27 queries .

回顶部