Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I copy a directory using Boost Filesystem

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.

like image 755
Ant Avatar asked Dec 21 '11 17:12

Ant


People also ask

Which method is used to copy a directory to another location ()?

copy() method in Python is used to copy the content of source file to destination file or directory.

What is Boost Filesystem?

The Boost Filesystem Library provides portable facilities to query and manipulate paths, files, and directories. The motivation for the library is the need to be able to perform portable script-like operations from within C++ programs.


2 Answers

bool copyDir(     boost::filesystem::path const & source,     boost::filesystem::path const & destination ) {     namespace fs = boost::filesystem;     try     {         // Check whether the function call is valid         if(             !fs::exists(source) ||             !fs::is_directory(source)         )         {             std::cerr << "Source directory " << source.string()                 << " does not exist or is not a directory." << '\n'             ;             return false;         }         if(fs::exists(destination))         {             std::cerr << "Destination directory " << destination.string()                 << " already exists." << '\n'             ;             return false;         }         // Create the destination directory         if(!fs::create_directory(destination))         {             std::cerr << "Unable to create destination directory"                 << destination.string() << '\n'             ;             return false;         }     }     catch(fs::filesystem_error const & e)     {         std::cerr << e.what() << '\n';         return false;     }     // Iterate through the source directory     for(         fs::directory_iterator file(source);         file != fs::directory_iterator(); ++file     )     {         try         {             fs::path current(file->path());             if(fs::is_directory(current))             {                 // Found directory: Recursion                 if(                     !copyDir(                         current,                         destination / current.filename()                     )                 )                 {                     return false;                 }             }             else             {                 // Found file: Copy                 fs::copy_file(                     current,                     destination / current.filename()                 );             }         }         catch(fs::filesystem_error const & e)         {             std:: cerr << e.what() << '\n';         }     }     return true; } 

Usage:

copyDir(boost::filesystem::path("/home/nijansen/test"), boost::filesystem::path("/home/nijansen/test_copy")); (Unix)

copyDir(boost::filesystem::path("C:\\Users\\nijansen\\test"), boost::filesystem::path("C:\\Users\\nijansen\\test2")); (Windows)

As far as I see, the worst that can happen is that nothing happens, but I won't promise anything! Use at your own risk.

Please note that the directory you're copying to must not exist. If directories within the directory you are trying to copy can't be read (think rights management), they will be skipped, but the other ones should still be copied.

Update

Refactored the function respective to the comments. Furthermore the function now returns a success result. It will return false if the requirements for the given directories or any directory within the source directory are not met, but not if a single file could not be copied.

like image 95
nijansen Avatar answered Sep 29 '22 05:09

nijansen


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 <exception> #include <filesystem> 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.

like image 32
Roi Danton Avatar answered Sep 29 '22 05:09

Roi Danton