Problem in Moving Folder in Qt
-
Hi, I want to move multi folders to other location (in same drive) and im using this code for doing this job
void MainWindow2::move() { int try_time = 1; while(try_time < 9) { try_time++; QString original = "D:/files/pro - start/SubProcess/" + QString::number(try_time); QString dest = "D:/files/pro - start/SubProcess/1"; QDir dir; if(!dir.rename( original, dest )) { qDebug() << "move failed"; } else { qDebug() << "file moved"; } } }
but this code only give me
move failed
and doesn't move any folder, what the problem? and how can i move folders? -
You try to rename a directory to the same name.
-
@saeid0034
I think my colleague @Christian-Ehrlicher read you code too quickly. Your first rename is fromSubProcess/2
toSubProcess/1
first time, so it's not the same name.Although I believe it is supposed to work starting from an "empty"
QDir dir
, you might just tryQDir dir("D:/files/pro - start/SubProcess"); dir.rename(QString::number(try_time), "1");
in case that makes a difference.
Otherwise a number of possibilities:
-
If your current directory is inside
QString::number(try_time)
this will fail. It will also fail if any other process has that as its current directory. Or if there is an open file handle intooriginal
. Or any subdirectories thereof, possibly. -
original
must exist. -
dest
must not exist. -
Your loop as shown does not exit after success, if that's all the code it will fail second time through the loop because
dest
will exist from first rename. You cannot "move multi folders to other location" per your code, asdest
must not exist --- what do you mean by "moving multi folders" to the same destination folder here?QDir::rename()
is a rename operation, not a move (as subdirectory) one. -
To debug I would try some "simpler" things, like
QDir::mkdir(dest)
, to see how that fares.
I can't help wondering whether you have the order of the rename the wrong way round? You are trying to rename from
SubProcess/{2,3,4,...}
toSubProcess/1
. Are you sure you're not wanting to rename fromSubProcess/1
to (the first available from)SubProcess/{2,3,4,...}
? Actually, perhaps not because of your "move multi folders to other location".... -
-
@saeid0034 Doing this in a loop is not a good idea!
Have you read QDir::rename() documentation?bool QDir::rename(const QString &oldName, const QString &newName)
Renames a file or directory from oldName to newName, and returns true if successful; otherwise returns false.
On most file systems, rename() fails only if oldName does not exist, or if a file with the new name already exists. However, there are also other reasons why rename() can fail. For example, on at least one file system rename() fails if newName points to an open file.
If oldName is a file (not a directory) that can't be renamed right away, Qt will try to copy oldName to newName and remove oldName.As you can see, this call will fail if destination path already exists... which will be at least the case on the second run!
-
@saeid0034
FromQDir::rename()
you must specify the path to the desired destination directory including that moved directory's new name, even if you want to keep it the same as it is now. So for your code that would be:QString original = "D:/files/pro - start/SubProcess/" + QString::number(try_time); QString dest = "D:/files/pro - start/SubProcess/1/" + QString::number(try_time);
So e.g.
SubProcess/2
would get renamed/moved toSubProcess/1/2
. Make sureSubProcess/1
exists before you start if you're not sure:dir.mkdir("D:/files/pro - start/SubProcess/1")
. -
@saeid0034
If you want to move folder/directory contents, not folder/directory itself, you have to iterate and move each file/subdirectory, e.g. something like (yes, you need to test/adjust!):dir = QDir(sourceDir); for (QFileInfo fileInfo : dir.entryList(QDir::NoDotAndDotDot)) dir.rename(fileInfo.fileName(), destDir + "/" fileInfo.fileName());
-
Hi I'm facing another problem in moving folders
because I open this topic before I think its better to ask question here againlet me say my problem with a example, i think its better
I have 9 folder with these names 1, 2, 3, 4, 5, 6, 7, 8 and all; all folder except all are full of files and subfolders (every folder have at least 5000 file).
How can i move all 8 folder files and subfolders to all folder? (also some subfolder in these folders have same name) -
@saeid0034 So did you actually tried something out?