How can I copy a directory using Boost Filesystem? I have tried boost::filesystem::copy_directory() but that only creates the target directory and does not copy the contents
Since C++17 you don't need boost for this operation anymore as filesystem has been added to the standard.
Use std::filesystem::copy
#include
#include
namespace fs = std::filesystem;
int main()
{
fs::path source = "path/to/source/folder";
fs::path target = "path/to/target/folder";
try {
fs::copy(source, target, fs::copy_options::recursive);
}
catch (std::exception& e) { // Not using fs::filesystem_error since std::bad_alloc can throw too.
// Handle exception or use error code overload of fs::copy.
}
}
See also std::filesystem::copy_options.