11、Qt:创建/删除文件夹、拷贝文件

一、创建文件夹

复制代码
声明:
bool createPath(QString strPath); 

定义:
/**
* @brief MainWindow::CreatePath 创建文件夹
* @param strPath 要创建的文件夹路径
* @return 创建成功返回true,失败返回false
*/
bool MainWindow::createPath(QString strPath)
{
    QDir dir;
    bool bExist = dir.exists(strPath);
    if(!bExist) //如果文件夹不存在
    {
        return dir.mkpath(strPath); //创建文件夹
    }

    return true;
}

二、删除当前目录及其下所有文件

复制代码
声明:
bool deleteDir(QString strPath); 

定义:
/**
* @brief MainWindow::deleteDir 删除当前目录及其下所有文件
* @param strPath 要删除的文件夹路径
* @return 删除成功返回true,失败返回false
*/
bool MainWindow::deleteDir(QString strPath)
{
    if(strPath.isEmpty())
    {
        return false;
    }

    QDir dir(strPath);
    if(!dir.exists()) //不是文件夹
    {
    return true;
    }

    //删除当前目录及其下所有文件
    return dir.removeRecursively();
}

三、把文件拷贝到指定路径中

复制代码
声明:
bool copyFileToPath(QString oldFile, QString newPath, bool isDelete = true); 

定义:
/**
* @brief MainWindow::copyFileToPath 把文件拷贝到指定路径中
* @param oldFile 要拷贝的文件
* @param newPath 目标路径
* @param isDelete 当目标路径中有同名文件时,是否删除,重新拷贝
* @return 拷贝成功返回true,失败返回false
*/
bool MainWindow::copyFileToPath(QString oldFile, QString newPath, bool isDelete)
{
    newPath.replace("\\", "/");

    QFileInfo fiSrc(oldFile);
    if(fiSrc.absoluteFilePath() == newPath) //文件的源路径与目标路径一样时,不需要再拷贝了
    {
        return true;
    }

    if(!createPath(newPath)) //创建文件夹失败
    {
        return false;
    }

    //判断newPath最后一个字符是否为"/"
    QString strNewFile; //新文件的路径加名称
    if(newPath.at(newPath.size()-1) == "/")
    {
        strNewFile = newPath + fiSrc.fileName();
    }
    else
    {
        strNewFile = newPath + "/" + fiSrc.fileName();
    }

    //判断文件是否存在
    QFile fiNew(strNewFile);
    if(fiNew.exists())
    {
        if(isDelete) //删除原有文件
        {
            fiNew.remove();
        }
        else
        {
            return true;
        }
    }

    //文件拷贝
    if(!QFile::copy(oldFile, strNewFile))
    {
        QString str = "拷贝文件失败:" + oldFile + "->" + strNewFile;
        qCritical() << str;
        deleteDir(newPath);
        return false;
    }
    return true;
}

四、获取文件夹下的文件

复制代码
声明:
bool findFile(QString path, QVector<QString> &fileNames, QString strSuffix = ""); 
定义:
/**
* @brief MainWindow::findFile 获取文件夹下的文件
* @param path 文件夹路径
* @param fileNames 文件集合
* @param strSuffix 文件后缀,只有指定后缀的文件,如.txt
* @return 获取成功返回true,失败返回false
*/
bool MainWindow::findFile(QString path, QVector<QString>& fileNames, QString strSuffix)
{
    QDir dir(path);
    if(!dir.exists()) //文件夹不存在
    {
        return false;
    }

    //获取filePath下所有文件夹和文件
    dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);//文件夹|文件|不包含./和../

    //排序文件夹优先
    dir.setSorting(QDir::DirsFirst);

    //获取文件夹下所有文件(文件夹+文件)
    QFileInfoList list = dir.entryInfoList();

    if(list.size() == 0) //没有文件
    {
        return false;
    }

    //遍历
    for(int i = 0; i < list.size(); i++)
    {
        QFileInfo fileInfo = list.at(i);

        if(fileInfo.isDir())//判断是否为文件夹
        {
            findFile(fileInfo.filePath(), fileNames, strSuffix); //递归开始
        }
        else
        {
            if(strSuffix == "")
            {
                fileNames.push_back(list.at(i).filePath()); //保存全部文件路径+文件名
            }
            else if(fileInfo.suffix() == strSuffix) //设定后缀
            {
                fileNames.push_back(list.at(i).filePath()); //保存全部文件路径+文件名
            }
        }
    }

    return true;
}

五、将源路径下所有文件拷贝到目的路径

复制代码
声明:
bool CopyPathFileToPath(QString oldPath, QString newPath, bool isDelete = true); 

定义:
/**
* @brief MainWindow::CopyPathFileToPath 将源路径下所有文件拷贝到目的路径
* @param oldPath 源路径
* @param newPath 目的路径
* @param bCoverFileIfExist 当目标路径中有同名文件时,是否删除,重新拷贝
* @return 拷贝成功返回true,失败返回false
*/
bool MainWindow::CopyPathFileToPath(QString oldPath, QString newPath, bool isDelete)
{
    newPath.replace("\\", "/");

    QVector<QString> vFileNames;
    if(!findFile(oldPath, vFileNames))
    {
        return false;
    }

    //oldPath中最后一个文件夹名 赋值给 newPath
    QString strName = oldPath.split("/").last();
    if(newPath.at(newPath.size()-1) == "/")
    {
        newPath = newPath + strName;
    }
    else
    {
        newPath = newPath + "/" + strName;
    }

    for(int i = 0; i < vFileNames.size(); ++i)
    {
        QString strPath = vFileNames.at(i);
        QString strTempDir = strPath.mid(oldPath.size());
        int nNamePos = strTempDir.lastIndexOf("/");
        QString strTempPath = strTempDir.left(nNamePos+1);
        QString strNewPath = newPath + strTempPath;

        if(!copyFileToPath(strPath, strNewPath, isDelete))  //拷贝失败
        {
            deleteDir(newPath);
            return false;
        }
    }

    return true;
}
相关推荐
爬虫程序猿2 小时前
用 Python 给京东商品详情做“全身 CT”——可量产、可扩展的爬虫实战
开发语言·爬虫·python
徐同保3 小时前
tailwindcss暗色主题切换
开发语言·前端·javascript
蓝纹绿茶4 小时前
bash:**:pip:***python: 错误的解释器: 没有那个文件或目录
开发语言·python·pip
云知谷4 小时前
【经典书籍】C++ Primer 第15章类虚函数与多态 “友元、异常和其他高级特性” 精华讲解
c语言·开发语言·c++·软件工程·团队开发
START_GAME4 小时前
深度学习Diffusers:用 DiffusionPipeline 实现图像生成
开发语言·python·深度学习
不爱编程的小九九5 小时前
小九源码-springboot088-宾馆客房管理系统
java·开发语言·spring boot
Evand J5 小时前
【MATLAB例程】到达角度定位(AOA),平面环境多锚点定位(自适应基站数量),动态轨迹使用EKF滤波优化。附代码下载链接
开发语言·matlab·平面·滤波·aoa·到达角度
细节控菜鸡6 小时前
【2025最新】ArcGIS for JS 实现随着时间变化而变化的热力图
开发语言·javascript·arcgis
Pluto_CSND6 小时前
Java实现gRPC双向流通信
java·开发语言·单元测试
原来是猿7 小时前
谈谈环境变量
java·开发语言