Copy folder qt c ++
- 
How can i copy a folder to another directory? 
- 
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
- 
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@mrjj tnks 
- 
@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::entryListwill return a large list, which might require large amount of memory.
 A better solution might be using QDirIterator withQDirIterator::Subdirectorieswhich uses resources only for one entry at a time.
- 
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(); }
 
