I have to copy the files from a source directory to a destination directory. Could you please provide the code for doing such operation
Asked
Active
Viewed 566 times
-1
-
4I believe this question has been answered in this post: http://stackoverflow.com/questions/2536524/copy-directory-using-qt – Edward Zhang Apr 21 '16 at 07:43
-
With Qt you mean qmake? – Frank Osterfeld Apr 21 '16 at 10:41
1 Answers
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;
}
Gleb Belyaev
- 70
- 8