Qt递归拷贝和删除目录


来源:Qt递归拷贝和删除目录



   最近在翻看项目代码时,看到了这两个函数,想到这个功能十分常用,因此拿出来与大家分享,希望对大家有用。几点说明:

1、记得当初写代码那会,是参考了网上的帖子写的,做了一点小修改。因此代码源于网络。

2、同时感谢原作者,只可惜当时没能记下原文网址,实在抱歉!刚才搜了一下,也没搜着,大家若发现原文出处,请跟帖提醒。谢谢!

3、到目前为止,代码在项目中测试、运行正常,大家若使用时发现Bug,请跟帖指出,我待验证后会及时修改更新。谢谢!

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. bool copyDir(const QString &source, const QString &destination, bool override)  
  2. {  
  3.     QDir directory(source);  
  4.     if (!directory.exists())  
  5.     {  
  6.         return false;  
  7.     }  
  8.   
  9.     QString srcPath = QDir::toNativeSeparators(source);  
  10.     if (!srcPath.endsWith(QDir::separator()))  
  11.         srcPath += QDir::separator();  
  12.     QString dstPath = QDir::toNativeSeparators(destination);  
  13.     if (!dstPath.endsWith(QDir::separator()))  
  14.         dstPath += QDir::separator();  
  15.   
  16.     bool error = false;  
  17.     QStringList fileNames = directory.entryList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);  
  18.     for (QStringList::size_type i=0; i != fileNames.size(); ++i)  
  19.     {  
  20.         QString fileName = fileNames.at(i);  
  21.         QString srcFilePath = srcPath + fileName;  
  22.         QString dstFilePath = dstPath + fileName;  
  23.         QFileInfo fileInfo(srcFilePath);  
  24.         if (fileInfo.isFile() || fileInfo.isSymLink())  
  25.         {  
  26.             if (override)  
  27.             {  
  28.                 QFile::setPermissions(dstFilePath, QFile::WriteOwner);  
  29.             }  
  30.             QFile::copy(srcFilePath, dstFilePath);  
  31.         }  
  32.         else if (fileInfo.isDir())  
  33.         {  
  34.             QDir dstDir(dstFilePath);  
  35.             dstDir.mkpath(dstFilePath);  
  36.             if (!copyDir(srcFilePath, dstFilePath, override))  
  37.             {  
  38.                 error = true;  
  39.             }  
  40.         }  
  41.     }  
  42.   
  43.     return !error;  
  44. }  


 QtCreator版本(在阅读 QtCreator 源码时,看到一个和以上功能一样的函数,想必像QtCreator这样的项目代码质量比我等程序员的代码质量更高。因此,特摘抄下来已做更新):

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // taken from utils/fileutils.cpp. We can not use utils here since that depends app_version.h.  
  2. static bool copyRecursively(const QString &srcFilePath,  
  3.                             const QString &tgtFilePath)  
  4. {  
  5.     QFileInfo srcFileInfo(srcFilePath);  
  6.     if (srcFileInfo.isDir()) {  
  7.         QDir targetDir(tgtFilePath);  
  8.         targetDir.cdUp();  
  9.         if (!targetDir.mkdir(QFileInfo(tgtFilePath).fileName()))  
  10.             return false;  
  11.         QDir sourceDir(srcFilePath);  
  12.         QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);  
  13.         foreach (const QString &fileName, fileNames) {  
  14.             const QString newSrcFilePath  
  15.                     = srcFilePath + QLatin1Char('/') + fileName;  
  16.             const QString newTgtFilePath  
  17.                     = tgtFilePath + QLatin1Char('/') + fileName;  
  18.             if (!copyRecursively(newSrcFilePath, newTgtFilePath))  
  19.                 return false;  
  20.         }  
  21.     } else {  
  22.         if (!QFile::copy(srcFilePath, tgtFilePath))  
  23.             return false;  
  24.     }  
  25.     return true;  
  26. }  


 //以下代码测试可用

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. bool deleteDir(const QString &dirName)  
  2. {  
  3.     QDir directory(dirName);  
  4.     if (!directory.exists())  
  5.     {  
  6.         return true;  
  7.     }  
  8.   
  9.     QString srcPath = QDir::toNativeSeparators(dirName);  
  10.     if (!srcPath.endsWith(QDir::separator()))  
  11.         srcPath += QDir::separator();  
  12.   
  13.     QStringList fileNames = directory.entryList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);  
  14.     bool error = false;  
  15.     for (QStringList::size_type i=0; i != fileNames.size(); ++i)  
  16.     {  
  17.         QString filePath = srcPath + fileNames.at(i);  
  18.         QFileInfo fileInfo(filePath);  
  19.         if (fileInfo.isFile() || fileInfo.isSymLink())  
  20.         {  
  21.             QFile::setPermissions(filePath, QFile::WriteOwner);  
  22.             if (!QFile::remove(filePath))  
  23.             {  
  24.                 qDebug() << "remove file" << filePath << " faild!";  
  25.                 error = true;  
  26.             }  
  27.         }  
  28.         else if (fileInfo.isDir())  
  29.         {  
  30.             if (!deleteDir(filePath))  
  31.             {  
  32.                 error = true;  
  33.             }  
  34.         }  
  35.     }  
  36.   
  37.     if (!directory.rmdir(QDir::toNativeSeparators(directory.path())))  
  38.     {  
  39.         qDebug() << "remove dir" << directory.path() << " faild!";  
  40.         error = true;  
  41.     }  
  42.   
  43.     return !error;  
  44. }  

 




  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值