备份目录到指定目录,qt代码实现
时间: 2023-06-01 17:05:38 浏览: 82
指定文件备份到某个目录下
以下是备份目录到指定目录的Qt代码实现:
```
#include <QCoreApplication>
#include <QDir>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString srcDir = "/path/to/source/directory/";
QString destDir = "/path/to/destination/directory/";
QDir dir;
if (!dir.exists(destDir)) {
if (!dir.mkpath(destDir)) {
qDebug() << "Failed to create destination directory!";
return 1;
}
}
QStringList fileList = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
foreach (QString file, fileList) {
QString srcFilePath = srcDir + file;
QString destFilePath = destDir + file;
if (!QFile::copy(srcFilePath, destFilePath)) {
qDebug() << "Failed to copy file " << srcFilePath << " to " << destFilePath;
return 1;
}
}
qDebug() << "Backup completed successfully!";
return 0;
}
```
请注意,这段代码只是一个示例,需要根据具体情况进行修改和完善。
阅读全文