Skip to content

Instantly share code, notes, and snippets.

@codeyash
Forked from ssendeavour/copy-recursively.cpp
Last active August 29, 2015 14:05
Show Gist options
  • Save codeyash/46692741aaba6adee4d5 to your computer and use it in GitHub Desktop.
Save codeyash/46692741aaba6adee4d5 to your computer and use it in GitHub Desktop.
/**
* @brief copyDir
* @param src
* @param dest
* @return bool
*
* Authors: wysota , Yash
* http://kineticwing.com
*
* License Apache 2.0
*
*/
bool copyDir(const QString &src, const QString &dest)
{
QDir dir(src);
QDir dirdest(dest);
if(!dir.isReadable())
return false;
QFileInfoList entries = dir.entryInfoList();
for(QList<QFileInfo>::iterator it = entries.begin(); it!=entries.end();++it)
{
QFileInfo &fileInfo = *it;
if(fileInfo.fileName()=="." || fileInfo.fileName()=="..")
{
continue;
}
else if(fileInfo.isDir())
{
qDebug() << dest + fileInfo.fileName() ;
QDir d;
d.mkdir(dest + "/" + fileInfo.fileName());
copyDir(fileInfo.filePath(),dest + "/" + fileInfo.fileName());
continue;
}
else if(fileInfo.isSymLink())
{
continue;
}
else if(fileInfo.isFile() && fileInfo.isReadable())
{
QFile file(fileInfo.filePath());
file.copy(dirdest.absoluteFilePath(fileInfo.fileName()));
}
else
return false;
}
return true;
}
or
//https://qt.gitorious.org/qt-creator/qt-creator/source/1a37da73abb60ad06b7e33983ca51b266be5910e:src/app/main.cpp#L13-189
// taken from utils/fileutils.cpp. We can not use utils here since that depends app_version.h.
static bool copyRecursively(const QString &srcFilePath,
const QString &tgtFilePath)
{
QFileInfo srcFileInfo(srcFilePath);
if (srcFileInfo.isDir()) {
QDir targetDir(tgtFilePath);
targetDir.cdUp();
if (!targetDir.mkdir(QFileInfo(tgtFilePath).fileName()))
return false;
QDir sourceDir(srcFilePath);
QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
foreach (const QString &fileName, fileNames) {
const QString newSrcFilePath
= srcFilePath + QLatin1Char('/') + fileName;
const QString newTgtFilePath
= tgtFilePath + QLatin1Char('/') + fileName;
if (!copyRecursively(newSrcFilePath, newTgtFilePath))
return false;
}
} else {
if (!QFile::copy(srcFilePath, tgtFilePath))
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment