Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Copy folder qt c ++
QtWS25 Last Chance

Copy folder qt c ++

Scheduled Pinned Locked Moved Solved Language Bindings
5 Posts 4 Posters 10.5k Views
  • 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 Offline
    N Offline
    Nathan Miguel
    wrote on last edited by
    #1

    How can i copy a folder to another directory?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      4
      • mrjjM mrjj

        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 Offline
        N Offline
        Nathan Miguel
        wrote on last edited by
        #3

        @mrjj tnks

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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
          5
          • J.HilkJ Online
            J.HilkJ Online
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            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


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

            1 Reply Last reply
            6

            • Login

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