• 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. }  

     http://blog.csdn.net/e5max/article/details/11923803

  • 相关阅读:
    微软企业库Enterprise Library学习笔记一
    ASP.net的地址重写(URLRewriter)实现原理及代码示例
    Naive Bayes text classification
    [转]select、poll、epoll的比较
    linux 异步file I/O操作示例
    [转]Linux动态库(.so)搜索路径
    [转]python urllib2
    What is Third party cookie definition
    linux 相关工具
    关于最大熵模型
  • 原文地址:https://www.cnblogs.com/findumars/p/5196236.html
Copyright © 2020-2023  润新知