Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Copy folder qt c ++

    Language Bindings
    4
    5
    7200
    Loading More Posts
    • 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.
    • N
      Nathan Miguel last edited by

      How can i copy a folder to another directory?

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        Hi
        You can use QFile::copy to copy all files to new folder.
        If you need to do it recursively, Qt has functions for that too that you can combine to a working function.
        https://stackoverflow.com/questions/2536524/copy-directory-using-qt

        N 1 Reply Last reply Reply Quote 4
        • N
          Nathan Miguel @mrjj last edited by

          @mrjj tnks

          1 Reply Last reply Reply Quote 0
          • Chris Kawa
            Chris Kawa Moderators last edited by

            @Nathan-Miguel Keep in mind that those stackoverflow solutions use recursion, which may result in (ironically) stack overflow with deeply nested directories. Also, if there's a lot of files QDir::entryList will return a large list, which might require large amount of memory.
            A better solution might be using QDirIterator with QDirIterator::Subdirectories which uses resources only for one entry at a time.

            1 Reply Last reply Reply Quote 5
            • J.Hilk
              J.Hilk Moderators last edited by

              Since QDir & QDir::EntryList has some serious trouble with (MacOS) app-bundles, I ended up implementing the QDirIterator solution @Chris-Kawa suggested:

              void copyAndReplaceFolderContents(const QString &fromDir, const QString &toDir, bool copyAndRemove = false) {
                  QDirIterator it(fromDir, QDirIterator::Subdirectories);
                  QDir dir(fromDir);
                  const int absSourcePathLength = dir.absoluteFilePath(fromDir).length();
              
                  while (it.hasNext()){
                      it.next();
                      const auto fileInfo = it.fileInfo();
                      if(!fileInfo.isHidden()) { //filters dot and dotdot
                          const QString subPathStructure = fileInfo.absoluteFilePath().mid(absSourcePathLength);
                          const QString constructedAbsolutePath = toDir + subPathStructure;
                          
                          if(fileInfo.isDir()){
                              //Create directory in target folder
                              dir.mkpath(constructedAbsolutePath);
                          } else if(fileInfo.isFile()) {
                              //Copy File to target directory
              
                              //Remove file at target location, if it exists, or QFile::copy will fail
                              QFile::remove(constructedAbsolutePath);
                              QFile::copy(fileInfo.absoluteFilePath(), constructedAbsolutePath);
                          }
                      }
                  }
              
                  if(copyAndRemove)
                      dir.removeRecursively();
              }
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

              Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply Reply Quote 6
              • First post
                Last post