Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem in Moving Folder in Qt
Forum Updated to NodeBB v4.3 + New Features

Problem in Moving Folder in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    saeid0034
    wrote on last edited by saeid0034
    #1

    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?

    JonBJ KroMignonK 2 Replies Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You try to rename a directory to the same name.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • S saeid0034

        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?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @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"....

        S 1 Reply Last reply
        1
        • S saeid0034

          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?

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #4

          @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!

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          4
          • JonBJ JonB

            @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"....

            S Offline
            S Offline
            saeid0034
            wrote on last edited by
            #5

            @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?

            JonBJ 1 Reply Last reply
            0
            • S saeid0034

              @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?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @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").

              S 1 Reply Last reply
              1
              • JonBJ JonB

                @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").

                S Offline
                S Offline
                saeid0034
                wrote on last edited by
                #7

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

                JonBJ 1 Reply Last reply
                0
                • S saeid0034

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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @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());
                  
                  1 Reply Last reply
                  2
                  • S Offline
                    S Offline
                    saeid0034
                    wrote on last edited by
                    #9

                    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)

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • S saeid0034

                      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)

                      Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @saeid0034 So did you actually tried something out?

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved