Important: Please read the Qt Code of Conduct -

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

  • Lifetime Qt Champion

    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 from SubProcess/2 to SubProcess/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 try

    QDir 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 into original. 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, as dest 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,...} to SubProcess/1. Are you sure you're not wanting to rename from SubProcess/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!

  • @JonB thanks for reply actually i want to move all files and folder from inside a folder to other exiting folder (and i want to do this job with multi folders as you see)
    i follow this answer.
    there is anyway to do this job?

  • @saeid0034
    From QDir::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 to SubProcess/1/2. Make sure SubProcess/1 exists before you start if you're not sure: dir.mkdir("D:/files/pro - start/SubProcess/1").

  • @JonB Thanks, and what if i wanted only move all folder and files inside of 1 folder? not move whole folder

  • @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 again

    let 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)

  • Lifetime Qt Champion

    @saeid0034 So did you actually tried something out?

Log in to reply