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



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



  • 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()



  • 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;
        }
    

  • Qt Champions 2016

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

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.