C++ C++;将文件从Qt传输到外部USB驱动器

C++ C++;将文件从Qt传输到外部USB驱动器,c++,qt,file-copying,C++,Qt,File Copying,我是Qt新手,需要帮助将所有文件从本地计算机的特定路径传输到外部USB驱动器。复制单个文件 你可以用 注意:此功能不会覆盖文件,因此必须删除以前存在的文件: if (QFile::exist(dstPath)) QFile::remove(dstPath); 如果需要显示用户界面以获取源路径和目标路径,可以使用的方法来实现。例如: bool copyFiles() { const QString srcPath = QFileDialog::getOpenFileName(this, "S

我是Qt新手,需要帮助将所有文件从本地计算机的特定路径传输到外部USB驱动器。

复制单个文件

你可以用

注意:此功能不会覆盖文件,因此必须删除以前存在的文件:

if (QFile::exist(dstPath)) QFile::remove(dstPath);
如果需要显示用户界面以获取源路径和目标路径,可以使用的方法来实现。例如:

bool copyFiles() {
  const QString srcPath = QFileDialog::getOpenFileName(this, "Source file", "",
    "All files (*.*)");
  if (srcPath.isNull()) return false; // QFileDialog dialogs return null if user canceled

  const QString dstPath = QFileDialog::getSaveFileName(this, "Destination file", "",
    "All files (*.*)"); // it asks the user for overwriting existing files
  if (dstPath.isNull()) return false;

  if (QFile::exist(dstPath))
    if (!QFile::remove(dstPath)) return false; // couldn't delete file
      // probably write-protected or insufficient privileges

  return QFile::copy(srcPath, dstPath);
}
复制目录的全部内容

我将答案扩展到
srcPath
是一个目录的情况。它必须手动和递归地完成。下面是执行此操作的代码,为简单起见,无需进行错误检查。你必须负责选择正确的方法(看看有什么想法)

void recursiveCopy(const QString& srcPath, const QString& dstPath) {
  QDir().mkpath(dstPath); // be sure path exists

  const QDir srcDir(srcPath);
  Q_FOREACH (const auto& dirName, srcDir.entryList(QStringList(), QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name)) {
    recursiveCopy(srcPath + "/" + dirName, dstPath + "/" + dirName);
  }

  Q_FOREACH (const auto& fileName, srcDir.entryList(QStringList(), QDir::Files, QDir::Name)) {
    QFile::copy(srcPath + "/" + fileName, dstPath + "/" + fileName);
  }
}
如果需要查询目录,可以使用

最后的评论


这两种方法都假定存在
srcPath
。如果使用
QFileDialog
方法,则很可能存在该方法(很可能是因为它不是原子操作,并且目录或文件可能在对话框和复制操作之间被删除或重命名,但这是另一个问题).

我已经解决了返回连接到机器的设备列表的问题。但除了Pendrive或HDD之外,所有设备都没有名称。所以
(!(storage.name()).isEmpty())
它将只返回这些设备的路径

QString location;
QString path1= "/Report/1.txt";
QString locationoffolder="/Report";

foreach (const QStorageInfo &storage, QStorageInfo::mountedVolumes()) {
    if (storage.isValid() && storage.isReady() && (!(storage.name()).isEmpty())) {
        if (!storage.isReadOnly()) {

           qDebug() << "path:" << storage.rootPath();
           //WILL CREATE A FILE IN A BUILD FOLDER
           location = storage.rootPath();
           QString srcPath = "writable.txt";
           //PATH OF THE FOLDER IN PENDRIVE
           QString destPath = location+path1;
           QString folderdir = location+locationoffolder;
           //IF FOLDER IS NOT IN PENDRIVE THEN IT WILL CREATE A FOLDER NAME REPORT
           QDir dir(folderdir);
           if(!dir.exists()){
              dir.mkpath(".");
           }

           qDebug() << "Usbpath:" <<destPath;
           if (QFile::exists(destPath)) QFile::remove(destPath);
           QFile::copy(srcPath,destPath);
           qDebug("copied");

        }
    }
}
QString位置;
QString path1=“/Report/1.txt”;
QString locationoffolder=“/Report”;
foreach(常量QStorageInfo&storage,QStorageInfo::mountedVolumes()){
if(storage.isValid()&&storage.isReady()&&(!(storage.name()).isEmpty()){
如果(!storage.isReadOnly()){

qDebug()你可能会发现这些链接很有用:请展示你的问题。你应该至少包括一个大纲(但最好是一个)您遇到问题的代码,然后我们可以尝试帮助解决具体问题。您也应该阅读。感谢帮助。我将使用它来解决我的问题。很高兴提供帮助,如果它解决了您的问题,请告诉我,如果您需要进一步帮助itIdk,我仍然无法解决问题。我还有文件的静态路径l至于我的外部驱动器,如QString srcPath=dir.relativeFilePath(“/home/untitled”);我没有使用它,因为它是静态的。我只是在头文件中声明source和dest文件夹,并调用QFile::copy(srcPath,dstPath);这在main.cpp.上考虑到,正如它的名字所说,相对于当前目录的路径。只需使用
QString srcPath=“/home/untitled”
.BTW,
QFile::copy
每次复制一个文件,如果你想复制一个完整的目录树,你必须迭代内容并手动创建它(或使用另一种方法,如
系统(“cp/R…”)
)。如果是这样,请将您的问题从“任何文件”修改为“所有文件”。
QString location;
QString path1= "/Report/1.txt";
QString locationoffolder="/Report";

foreach (const QStorageInfo &storage, QStorageInfo::mountedVolumes()) {
    if (storage.isValid() && storage.isReady() && (!(storage.name()).isEmpty())) {
        if (!storage.isReadOnly()) {

           qDebug() << "path:" << storage.rootPath();
           //WILL CREATE A FILE IN A BUILD FOLDER
           location = storage.rootPath();
           QString srcPath = "writable.txt";
           //PATH OF THE FOLDER IN PENDRIVE
           QString destPath = location+path1;
           QString folderdir = location+locationoffolder;
           //IF FOLDER IS NOT IN PENDRIVE THEN IT WILL CREATE A FOLDER NAME REPORT
           QDir dir(folderdir);
           if(!dir.exists()){
              dir.mkpath(".");
           }

           qDebug() << "Usbpath:" <<destPath;
           if (QFile::exists(destPath)) QFile::remove(destPath);
           QFile::copy(srcPath,destPath);
           qDebug("copied");

        }
    }
}