bool Qaul::QaulCopyDir(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 (!this->QaulCopyDir(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; }
bool copyDir(const QString &srcPath, const QString &dstPath) { bool result = true; bool test = true; LOG_FREE(Info, "copyDir", "copyDir '" << toString(srcPath) << "' to '" << toString(dstPath) << "'"); // ensure directory exists QFileInfo dstInfo(dstPath); QString fileName = dstInfo.fileName(); if (!dstInfo.exists() || !dstInfo.isDir()){ QDir parentDstDir(QFileInfo(dstPath).path()); LOG_FREE(Info, "copyDir", "Creating directory named = '" << toString(fileName) << "' in parentDstDir = '" << toString(parentDstDir.path()) << "'"); if (!parentDstDir.mkpath(fileName)){ LOG_FREE(Error, "copyDir", "Failed to create directory = '" << toString(fileName) << "' in parentDstDir = '" << toString(parentDstDir.path()) << "'"); return false; } } QDir srcDir(srcPath); // remove all files in dst as well as any directories in dst that are not in src test = synchDirStructures(srcPath, dstPath); if (!test){ LOG_FREE(Error, "copyDir", "Failed to synch destination '" << toString(dstPath) << "' with source '" << toString(srcPath) << "'"); result = false; } // copy all files in src to dst for (const QFileInfo &srcItemInfo : srcDir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot)) { QString srcItemPath = srcPath + "/" + srcItemInfo.fileName(); QString dstItemPath = dstPath + "/" + srcItemInfo.fileName(); QFileInfo dstItemInfo = QFileInfo(dstItemPath); if (srcItemInfo.isDir()) { test = copyDir(srcItemPath, dstItemPath); if (!test){ LOG_FREE(Error, "copyDir", "Failed to copy directory '" << toString(srcItemPath) << "' to '" << toString(dstItemPath) << "'"); result = false; // DLM: do we really want to give up here? //return false; } } else if (srcItemInfo.isFile()) { test = QFile::copy(srcItemPath, dstItemPath); if (!test){ LOG_FREE(Error, "copyDir", "Failed to copy file '" << toString(srcItemPath) << "' to '" << toString(dstItemPath) << "'"); result = false; // DLM: do we really want to give up here? //return false; } } } return result; }
bool Util::copyDir(const QString &srcPath, const QString &dstPath, bool overwrite) { QDir parentDstDir(QFileInfo(dstPath).path()); parentDstDir.mkdir(QFileInfo(dstPath).fileName()); QFileInfo srcInfo(srcPath); if (srcInfo.isFile()) { QString dstItemPath = dstPath + "/" + srcInfo.fileName(); if (QFile::exists(dstItemPath) && overwrite) QFile::remove(dstItemPath); return QFile::copy(srcPath, dstItemPath); } 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 (!copyDir(srcItemPath, dstItemPath, overwrite)) { return false; } } else if (info.isFile()) { if (QFile::exists(dstItemPath) && overwrite) QFile::remove(dstItemPath); if (!QFile::copy(srcItemPath, dstItemPath)) { return false; } } } return true; }