-1

I have to copy the files from a source directory to a destination directory. Could you please provide the code for doing such operation

1 Answers1

0

I wanted something similar, and was googling (in vain), so this is where I've got to:

 static bool cpDir(const QString &srcPath, const QString &dstPath)
    {
        rmDir(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;
    }