当前位置: 首页 > news >正文

QT文件和文件夹拷贝操作

1.拷贝文件夹

//(源文件目录路劲,目的文件目录,文件存在是否覆盖)
bool copyDirectory(const QString& srcPath, const QString& dstPath, bool coverFileIfExist)
{
    QDir srcDir(srcPath);
    QDir dstDir(dstPath);
    if (!dstDir.exists()) { //目的文件目录不存在则创建文件目录
        if (!dstDir.mkdir(dstDir.absolutePath()))
            return false;
    }
    QFileInfoList fileInfoList = srcDir.entryInfoList();
    foreach(QFileInfo fileInfo, fileInfoList) {
        if (fileInfo.fileName() == "." || fileInfo.fileName() == "..")
            continue;

        if (fileInfo.isDir()) {    // 当为目录时,递归的进行copy 
            if (!copyDirectory(fileInfo.filePath(),dstDir.filePath(fileInfo.fileName()),coverFileIfExist))
                return false;
        }
        else {            //当允许覆盖操作时,将旧文件进行删除操作 
            if (coverFileIfExist && dstDir.exists(fileInfo.fileName())) {
                dstDir.remove(fileInfo.fileName());
            }
            /// 进行文件copy
            if (!QFile::copy(fileInfo.filePath(), dstDir.filePath(fileInfo.fileName()))) {
                return false;
            }
        }
    }
    return true;
}

2.拷贝文件

//(源文件文件路径,目的文件路径,文件存在是否覆盖)
bool copyFile(QString srcPath, QString dstPath, bool coverFileIfExist)
{
    srcPath.replace("\\", "/");
    dstPath.replace("\\", "/");
    if (srcPath == dstPath) {
        return true;
    }

    if (!QFile::exists(srcPath)) {  //源文件不存在
        return false;
    }

    if (QFile::exists(dstPath)) {
        if (coverFileIfExist) {
            QFile::remove(dstPath);
        }
    }

    if (!QFile::copy(srcPath, dstPath)){
        return false;
    }
    return true;
}
 

相关文章:

  • NetApp ONTAP 9 故障磁盘更换操作指南
  • 《零基础入门学习Python》
  • OpenGL shader开发实战学习笔记:第十二章 深入光照
  • Linux指令合集
  • C++ STL:从零开始模拟实现 list 容器
  • ACWing——算法基础课
  • 棉花糖实验新解
  • Linux-scp命令
  • 3DMAX 常用渲染器对比及渲染 101 云平台优势
  • (EtherNet/IP 转 EtherNet/IP)EtherCAT/Ethernet/IP/Profinet/ModbusTCP协议互转工业串口网关
  • 存储器综合:内存条
  • 【NLP 69、KG - BERT】
  • linux sysfs的使用
  • μC/OS 版本演进过程 | uC/OS-II 和 uC/OS-III 有什么区别?
  • 运维:概念、模式与硬件基础
  • 使用Python可视化莫比乌斯带
  • 【leetcode题解】滑动窗口
  • STM32 栈地址起始位置不正确
  • V5验证官网滑块验证码WSS协议逆向算法分析
  • 【知识】性能优化和内存优化的主要方向
  • 空山日落雨初收,来文徵明的画中听泉
  • 中国与柬埔寨签署产供链经济合作谅解备忘录
  • 内部敏感文件遭万人共享,特朗普政府又曝安全漏洞
  • 2025中国互联网企业家座谈会在京召开
  • 调查显示特朗普在经济问题上的支持率跌至其总统生涯最低
  • “80后”辽宁石油化工大学副校长杨占旭已任阜新市领导