Qt笔记-windows下拷贝文件夹中所有文件

这里其实各个平台都通用,但个人感觉,如果是linux,直接用shell命名。更加方便。

这里逻辑是这样的。

递归函数,直接先找到最里面,把文件拷贝。然后逐步出来即可!

下面是代码:

generatefile.h

#ifndef GENERATEFILE_H
#define GENERATEFILE_H

#include <QObject>

class GenerateFile : public QObject
{
    Q_OBJECT
public:
    explicit GenerateFile(QObject *parent = 0);
    static bool cpDir(const QString &srcPath, const QString &dstPath);
	......
    ......

signals:

public slots:
};

#endif // GENERATEFILE_H

generatefile.cpp

#include "generatefile.h"
#include <QDir>
#include <QSettings>
#include <QDebug>


GenerateFile::GenerateFile(QObject *parent) : QObject(parent)
{

}

bool GenerateFile::cpDir(const QString &srcPath, const QString &dstPath)
{
    QDir parentDstDir(QFileInfo(dstPath).path());
    if(!parentDstDir.mkdir(QFileInfo(dstPath).fileName())){
  
        return false;        
    }
  
    QDir srcDir(srcPath);
    foreach(const QFileInfo &info, srcDir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot)){
        
        QString srcItemPath = srcPath + "/" + info.fileName();
        QString dstItemPath = dstPath + "/" + info.fileName();
        if(info.isDir()){

            if(!cpDir(srcItemPath, dstItemPath)){

                return false;
            }
        }
        else if(info.isFile()){

            if(!QFile::copy(srcItemPath, dstItemPath)){

                return false;
            }
        }
        else{

            qDebug() << "Unhandled item" << info.filePath() << "in cpDir";
        }
    }

    return true;
}

......
......
发布了1269 篇原创文章 · 获赞 1970 · 访问量 179万+

猜你喜欢

转载自blog.csdn.net/qq78442761/article/details/104393690
今日推荐