Jimit Rupani
Jimit Rupani

Reputation: 510

C++ Transfer Files From Qt to External USB Drive

I am new in Qt and I need help in transferring all files from a specific path of the local machine to an external USB Drive.

Upvotes: 2

Views: 4020

Answers (2)

Jimit Rupani
Jimit Rupani

Reputation: 510

I have solved the problem with the QStorageInfo::mountedVolumes() which return the list of the devices that are connected to the Machine. But all of them won't have a name except the Pendrive or HDD. So (!(storage.name()).isEmpty())) it will return the path to only those devices.

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

        }
    }
}

I had to create a folder as well as in USB because of my requirements and I have given a static name for the files. Then I just copied the data from file of the local machine to the file which I have created in USB with the help of QFile::copy(srcPath, dstPath). I hope it will help someone.

Upvotes: 3

cbuchart
cbuchart

Reputation: 11555

Copying a single file

You can use QFile::copy.

QFile::copy(srcPath, dstPath);

Note: this function doesn't overwrite files, so you must delete previous files if they exist:

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

If you need to show an user interface to get the source and destination paths, you can use QFileDialog's methods to do that. Example:

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

Copying the whole content of a directory

I'm extending the answer to the case srcPath is a directory. It must be done manually and recursively. Here is the code to do it, without error checking for simplicity. You must be in charge of choosing the right method (take a look at QFileInfo::isFile for some ideas.

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

If you need to ask for the directory, you can use QFileDialog::getExistingDirectory.

Final remarks

Both methods assume srcPath exists. If you used the QFileDialog methods it is highly probable that it exists (highly probable because it is not an atomic operation and the directory or file may be deleted or renamed between the dialog and the copy operation, but this is a different issue).

Upvotes: 4

Related Questions