天天看點

Qt之複制檔案夾

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 (srcFileInfo.fileName().contains("oadata.db", Qt::CaseInsensitive) ||
			srcFileInfo.fileName().contains("basedata.db", Qt::CaseInsensitive) ||
			srcFileInfo.fileName().contains("account.db", Qt::CaseInsensitive) ||
			srcFileInfo.fileName().contains("config.db", Qt::CaseInsensitive) 
		{
			if (!QFile::copy(srcFilePath, tgtFilePath))
				return false;
		}
	}
	return true;
}
           

上面這段代碼是将檔案夾srcFilePath中的資料庫檔案(*.db)複制到目标檔案夾tgtFilePath中,如果要複制檔案夾中的所有檔案去掉if(srcFileInfo.fileName()......)的條件判斷即可。

Qt之複制檔案夾

繼續閱讀