Qt中复制文件夹中所有内容

目录

 

1.自己的实现版本: 比较啰嗦

2.stackoverflow上的高赞答案

 


1.自己的实现版本: 比较啰嗦

//复制一个文件中的全部内容到另一个文件夹中
void CopyFiles(QString strSour, QString strDest)
{
    if(true == strSour.endsWith('/'))
    {
        strSour.chop(1);
    }
    if(true == strDest.endsWith('/'))
    {
        strDest.chop(1);
    }

    QDir dirDest(strDest);

    //1.对参数进行判断,看是否存在路径
    if(strSour.isEmpty() || strDest.isEmpty())
    {
        qDebug() << "传入的路径存在空的情况";
        return;
    }

    //2.遍历strSour,得到完整的文件列表
    QStringList strListTotal;
    traversal(strSour, strListTotal);
    qDebug() << "size:" << strListTotal.size();
    qDebug() << "strListTotal:" << strListTotal;

    //3.遍历列表,在目的目录下创建文件夹或把文件粘贴过去
    int i = 0;
    foreach(const QString & str, strListTotal)
    {
        QString strTmp = str;
        QString strRear = strTmp.remove((strSour+"/"));//! 这里需要保证strSour以"/"结尾.
        qDebug() << "strTmp:" << strTmp;
        int nSlash = strRear.count('/');
        bool bEndWithSlash = strRear.endsWith('/');
        //如果不含'/',且strRear不为空,直接复制
        if(0 == nSlash && false == strRear.isEmpty())
        {
            QFile::copy(str, strDest + "/" + strRear);
        }

        if(nSlash > 0)
        {
            if(bEndWithSlash)
            {
                QStringList strListRear = strRear.split('/');
                QString strCombine = strDest;
                //! 保证前一级的文件夹的存在
                for(const QString &str: strListRear)
                {
                    if(false == str.isEmpty())
                    {
                        strCombine = strCombine + "/" + str;
                        if(!dirDest.exists(strCombine))
                        {
                            dirDest.mkdir(strCombine);
                        }
                    }
                }
            }
            else
            {
                bool bCopy = QFile::copy(str, strDest + "/" + strRear);
                qDebug() << "str:" << str;
                qDebug() << "bCopy:" << bCopy;
                qDebug() << "strDest + strRear:" << (strDest + "/" + strRear);
                qDebug() << i++;
            }
        }
    }
}

void traversal(QString path, QStringList& strListTotal)
{
    if(QDir(path).exists())
    {
        QString dirPath = path + "/";
        strListTotal << dirPath;
        QStringList strListPath = QDir(path).entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot, QDir::DirsFirst);//此处不可以仅使用NoDotAndDotDot,否则得到空列表
        for(QString itemPath : strListPath)
        {
            qDebug() << "itemPath:" << itemPath;
            QString absPath = path + "/" + itemPath;
            traversal(absPath, strListTotal);
        }
    }
    else
    {
        strListTotal << path;
    }
}

void CopyFilesRecursive(QString strSour, QString strDest)
{
    if(strSour.isEmpty())
    {
        return;
    }
    QDir dirSour(strSour);
    QDir dirDest(strDest);
    QStringList strList = dirSour.entryList(QDir::Dirs| QDir::Files | QDir::NoDotAndDotDot, QDir::DirsFirst);
    for(const QString &str: strList)
    {
        QString fullPath = strSour + QDir::separator() + str;
        QFileInfo fileInfo(fullPath);
        if(fileInfo.isDir())
        {
            dirDest.mkdir(str);
            CopyFilesRecursive(fullPath, strDest + QDir::separator() + str);//保持目录级别一致
        }
        else
        {
            QFile::copy(fullPath, strDest + QDir::separator() + str);//需要保证目标目录存在,文件才能copy成功
        }
    }
    qDebug() << "info";
}

2.stackoverflow上的高赞答案

void copyPath(QString src, QString dst)
{
    QDir dir(src);
    if (! dir.exists())
        return;

    foreach (QString d, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
        QString dst_path = dst + QDir::separator() + d;
        dir.mkpath(dst_path);
        copyPath(src+ QDir::separator() + d, dst_path);//use recursion
    }

    foreach (QString f, dir.entryList(QDir::Files)) {
        QFile::copy(src + QDir::separator() + f, dst + QDir::separator() + f);
    }
}

链接:https://stackoverflow.com/questions/2536524/copy-directory-using-qt

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Qt,可以使用QFile和QDir两个类来实现将一个文件复制文件夹的功能。 首先,我们需要创建一个QFile对象来表示要复制的源文件,以及一个QDir对象来表示目标文件夹。然后,使用QFile的copy()函数将源文件复制到目标文件夹。 下面是一个具体的代码示例: ```cpp QString sourceFilePath = "/path/to/source/file.txt"; // 源文件路径 QString destinationFolderPath = "/path/to/destination/folder"; // 目标文件夹路径 QFile sourceFile(sourceFilePath); QDir destinationFolder(destinationFolderPath); // 检查源文件和目标文件夹是否存在 if (sourceFile.exists() && destinationFolder.exists()) { // 获取源文件名 QString fileName = sourceFile.fileName(); // 拼接目标文件路径 QString destinationFilePath = destinationFolderPath + "/" + fileName; // 复制文件 if (sourceFile.copy(destinationFilePath)) { qDebug() << "文件复制成功!"; } else { qDebug() << "文件复制失败!"; } } else { qDebug() << "源文件或目标文件夹不存在!"; } ``` 在这个示例,我们首先创建了QFile和QDir对象,然后检查源文件和目标文件夹是否存在。如果存在,就通过拼接目标文件路径来复制文件。复制成功后,会输出"文件复制成功!";如果复制失败或源文件或目标文件夹不存在,会相应地输出相应的提示信息。 这是一个基本的示例,你可以根据实际需求做进一步的适应或扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodingLife99

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值