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. is there any API to recursively copy a Directory and all it's Sub Dirs and Files?
Forum Updated to NodeBB v4.3 + New Features

is there any API to recursively copy a Directory and all it's Sub Dirs and Files?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 6.1k Views 4 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.
  • O Offline
    O Offline
    opengpu2
    wrote on last edited by
    #1

    is there any API to recursively copy a Directory and all it's Sub Dirs and Files?
    thanks

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      as far as I know the answer is NO.
      I think you have to write your own code using QDir::entryInfoList(), QDir::makePath() and QFile::copy()

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      1
      • L Offline
        L Offline
        Larpon
        wrote on last edited by
        #3

        For any lost Googlers - looking for some quick copy-paste.

        I've had success with this function :)

            bool copyRecursively(QString sourceFolder, QString destFolder)
            {
                bool success = false;
                QDir sourceDir(sourceFolder);
        
                if(!sourceDir.exists())
                    return false;
        
                QDir destDir(destFolder);
                if(!destDir.exists())
                    destDir.mkdir(destFolder);
        
                QStringList files = sourceDir.entryList(QDir::Files);
                for(int i = 0; i< files.count(); i++) {
                    QString srcName = sourceFolder + QDir::separator() + files[i];
                    QString destName = destFolder + QDir::separator() + files[i];
                    success = QFile::copy(srcName, destName);
                    if(!success)
                        return false;
                }
        
                files.clear();
                files = sourceDir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
                for(int i = 0; i< files.count(); i++)
                {
                    QString srcName = sourceFolder + QDir::separator() + files[i];
                    QString destName = destFolder + QDir::separator() + files[i];
                    success = copyRecursively(srcName, destName);
                    if(!success)
                        return false;
                }
        
                return true;
            }
        
        1 Reply Last reply
        1
        • benlauB Offline
          benlauB Offline
          benlau
          Qt Champions 2016
          wrote on last edited by
          #4

          Qt do not support recursively copy of a directory. Instead, you may take a look with my library , QtShell, it could manipulate files by a shell command style API. It supports a recursively copy of files and handled several more conditions:

          https://github.com/benlau/qtshell#cp

          Example

          cp("-a", ":/*", "/target"); // copy all files from qrc resource to target path recursively
          
          cp("tmp.txt", "/tmp");
          
          cp("*.txt", "/tmp");
          
          cp("/tmp/123.txt", "456.txt");
          
          cp("-va","src/*", "/tmp");
          
          1 Reply Last reply
          2

          • Login

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