2017-04-07 72 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); **。 –