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.
-
You might find these links useful: http://doc.qt.io/qt-4.8/qfiledialog.html and http://stackoverflow.com/questions/19928216/qt-copy-a-file-from-one-directory-to-another – ni1ight Apr 07 '17 at 07:45
-
2Please [edit] your question to show [the code you have so far](http://whathaveyoutried.com). You should include at least an outline (but preferably a [mcve]) of the code that you are having problems with, then we can try to help with the specific problem. You should also read [ask]. – Toby Speight Apr 07 '17 at 10:39
2 Answers
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).

- 10,847
- 9
- 53
- 93
-
-
Glad to help, let me know if it solves your problem and if you need further help with it – cbuchart Apr 07 '17 at 09:26
-
Idk I am still stuck on the problem. I have the static path for the file as well as for my external drive like **QString srcPath = dir.relativeFilePath("/home/untitled");** . I am not using [QFileDialog](http://doc.qt.io/qt-5/qfiledialog.html) because of the static thing. I am just declaring source and dest folder in header file and calling **QFile::copy(srcPath, dstPath);** this on main.cpp. – Jimit Rupani Apr 07 '17 at 11:15
-
[`QDir::relativeFilePath`](http://doc.qt.io/qt-5/qdir.html#relativeFilePath) takes into consideration, as its name says, a path relative to current directory. Just use `QString srcPath = "/home/untitled";`. – cbuchart Apr 07 '17 at 11:28
-
BTW, `QFile::copy` copies a single file each time, if you want to copy a full directory tree, you must iterative through content and make it manually (or use another method like a `system("cp /R ...")`). If so, please modify your question from "any files" to "all files". – cbuchart Apr 07 '17 at 11:31
-
1I've added a method to make the recursive copy too, take a look at it (sorry if it has any mistake, I typed it quickly and couldn't validate all the cases). – cbuchart Apr 07 '17 at 11:40
-
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.

- 10,847
- 9
- 53
- 93

- 498
- 6
- 15