免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6958 | 回复: 1
打印 上一主题 下一主题

Qt 实现的拷贝文件夹的函数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-15 16:50 |只看该作者 |倒序浏览
  1. #include <QDir>
  2. #include <QFileInfoList>

  3. /**
  4.   qCopyDirectory -- 拷贝目录
  5.   fromDir : 源目录
  6.   toDir   : 目标目录
  7.   bCoverIfFileExists : ture:同名时覆盖  false:同名时返回false,终止拷贝
  8.   返回: ture拷贝成功 false:拷贝未完成
  9. */
  10. bool qCopyDirectory(const QDir& fromDir, const QDir& toDir, bool bCoverIfFileExists)
  11. {
  12.     QDir formDir_ = fromDir;
  13.     QDir toDir_ = toDir;

  14.     if(!toDir_.exists())
  15.     {
  16.         if(!toDir_.mkdir(toDir.absolutePath()))
  17.             return false;
  18.     }

  19.     QFileInfoList fileInfoList = formDir_.entryInfoList();
  20.     foreach(QFileInfo fileInfo, fileInfoList)
  21.     {
  22.         if(fileInfo.fileName() == "." || fileInfo.fileName() == "..")
  23.             continue;

  24.         //拷贝子目录
  25.         if(fileInfo.isDir())
  26.         {
  27.             //递归调用拷贝
  28.             if(!qCopyDirectory(fileInfo.filePath(), toDir_.filePath(fileInfo.fileName())))
  29.                 return false;
  30.         }
  31.         //拷贝子文件
  32.         else
  33.         {
  34.             if(bCoverIfFileExists && toDir_.exists(fileInfo.fileName()))
  35.             {
  36.                 toDir_.remove(fileInfo.fileName());
  37.             }
  38.             if(!QFile::copy(fileInfo.filePath(), toDir_.filePath(fileInfo.fileName())))
  39.             {
  40.                 return false;
  41.             }
  42.         }
  43.     }
  44.     return true;
  45. }
复制代码

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:56:11
2 [报告]
发表于 2011-03-19 17:28 |只看该作者
还真没有想过用qt作这个功能阿, 一般就做下界面。功能全是不同的平台上自己写。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP