2017-04-07 70 views
1

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

+0

您可能会发现这些链接很有用:http://doc.qt.io/qt-4.8/qfiledialog.html和http://stackoverflow.com/questions/19928216/qt-copy-a-file-from-一个目录到另一个目录 – ni1ight

+1

请[编辑]你的问题以显示[迄今为止的代码](http://whathaveyoutried.com)。你至少应该包括一个你遇到问题的代码大纲(但最好是[mcve]),然后我们可以尝试帮助解决具体问题。你还应该阅读[问]。 –

回答

2

我已经解决了QStorageInfo::mountedVolumes()这个问题,它返回连接到机器的设备列表。但是除了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"); 

     } 
    } 
} 

因为我的要求,我必须创建一个文件夹以及在USB中,我给了文件的静态名称。然后,我只是从本地机器的文件中复制数据到我在USB中创建的文件中,在QFile::copy(srcPath, dstPath)的帮助下。我希望它能帮助别人。

3

复制单个文件

您可以使用QFile::copy

QFile::copy(srcPath, dstPath); 

注:此功能覆盖文件,因此,如果他们存在,您必须删除以前的文件:

if (QFile::exist(dstPath)) QFile::remove(dstPath); 

如果你需要显示的用户界面,以获得源和目的地路径,你可以使用QFileDialog的方法来做到这一点。例如:

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的全部内容是一个目录。它必须手动和递归地完成。这是执行此操作的代码,无需为简单起见进行错误检查。您必须负责选择合适的方法(看看QFileInfo::isFile的一些想法的。

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); 
    } 
} 

如果你需要问的目录,你可以使用QFileDialog::getExistingDirectory

结束语

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

+0

感谢您的帮助,我会用它来解决我的问题。 –

+0

很高兴帮助,让我知道它是否解决了您的问题,如果您需要进一步的帮助 – cbuchart

+0

Idk我仍然停留在这个问题上。我有文件以及外部驱动器的静态路径,如** QString srcPath = dir.relativeFilePath(“/ home/untitled”); **。我没有使用[QFileDialog](http://doc.qt.io/qt-5/qfiledialog。HTML)因为静态的东西。我只是在头文件中声明源文件和目标文件夹,并在main.cpp中调用** QFile :: copy(srcPath,dstPath); **。 –