How can I copy a directory using Boost Filesystem

boost::filesystem::copy directory
c++ copy directory
boost::filesystem::exists
std filesystem copy directory
boost/filesystem path relative
boost path append
boost filesystem create_directory
boost filesystem create_directories vs create_directory

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.

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.

boost::filesystem recursive directory copy · GitHub, boost::filesystem recursive directory copy. recursive_copy.cpp. using bfs = boost::​filesystem;. void recursive_copy(const bfs::path &src, const bfs::path &dst). boost filesystem tutorial directory iteration boost filesystem reference copy_directory and copy_file boost filesystem changed to version 3 recently. take care not to follow V2 tutorials.

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.

Filesystem Reference, https://www.boost.org › doc › libs › libs › filesystem › doc › reference Since you use experimental::filesystem::copy_options::none you will get an exception if the file already exists - see the paragraph "Otherwise, if the destination file already exists" here If the source folder contains subfolders with files, the directory_iterator will iterate them as well, but if you attempt to copy a file nested deeper, you need to create the target directory hierarchy as well.

I see this version as an improved upon version of @nijansen's answer. It also supports the source and/or destination directories to be relative.

namespace fs = boost::filesystem;

void copyDirectoryRecursively(const fs::path& sourceDir, const fs::path& destinationDir)
{
    if (!fs::exists(sourceDir) || !fs::is_directory(sourceDir))
    {
        throw std::runtime_error("Source directory " + sourceDir.string() + " does not exist or is not a directory");
    }
    if (fs::exists(destinationDir))
    {
        throw std::runtime_error("Destination directory " + destinationDir.string() + " already exists");
    }
    if (!fs::create_directory(destinationDir))
    {
        throw std::runtime_error("Cannot create destination directory " + destinationDir.string());
    }

    for (const auto& dirEnt : fs::recursive_directory_iterator{sourceDir})
    {
        const auto& path = dirEnt.path();
        auto relativePathStr = path.string();
        boost::replace_first(relativePathStr, sourceDir.string(), "");
        fs::copy(path, destinationDir / relativePathStr);
    }
}

The main differences are exceptions instead of return values, the use of recursive_directory_iterator and boost::replace_first to strip the common part of the iterator path, and relying on boost::filesystem::copy() to do the right thing with different file types (preserving symlinks, for instance).

Using boost::filesystem, This recursively uses boost::filesystem::create_directory to create a copy of a directory including its contents, sub-directories  Boost.Filesystem includes status query functions such as exists, is_directory, and is_regular_file. These return bool's, and will return true if the condition described by their name is met. Otherwise they return false, including when any element of the path argument can't be found.

This is a non-Boost version I'm using based on Doineann's code. I'm using std::filesystem but couldn't use a simple fs::copy(src, dst, fs::copy_options::recursive); because I wanted to filter which files are copied by file extension inside the loop.

void CopyRecursive(fs::path src, fs::path dst)
{
    //Loop through all the dirs
    for (auto dir : fs::recursive_directory_iterator(src))
    {
        //copy the path's string to store relative path string
        std::wstring relstr = dir.path().wstring();

        //remove the substring matching the src path
        //this leaves only the relative path
        relstr.erase(0, std::wstring(src).size());

        //combine the destination root path with relative path
        fs::path newFullPath = dst / relstr;

        //Create dir if it's a dir
        if (fs::is_directory(newFullPath))
        {
            fs::create_directory(newFullPath);
        }

        //copy the files
        fs::copy(dir.path(), newFullPath, fs::copy_options::recursive | fs::copy_options::overwrite_existing);
    }
}

relstr.erase(0, std::wstring(src).size()); is a working Boost-less replacement for the boost::replace_first() call used in the other answer

Chapter 35. Boost.Filesystem, The member functions presented with boost::filesystem::path simply process symbolic links or copy_file() and copy_directory() to copy files and directories are​  Filesystem library functions often provide two overloads, one that throws an exception to report file system errors, and another that sets an error_code. [Note: This supports two common use cases: Uses where file system errors are truly exceptional and indicate a serious failure. Throwing an exception is the most appropriate response.

std::filesystem::copy, Otherwise, if from is a directory and copy_options::create_symlinks is set in options , reports an error with an error code equal to std::  How should I use directory_iterator to list directory files (not recursive)?. Also what header files / libs should I add/link or other settings I should make? I'm using boost in my project but by some reason directory_iterator is "underclared identifier" while I can use other boost features.

Users - How do I copy files and folders using boost and , Visual Studio 2005?. I'm trying to use boost::filesystem to copy files and folders (just like a standard copy a folder and paste it in windows One possible explanation is that Boost was compiled with a different standard library than what you're compiling your program with, as suggested by the issue crash when using boost 1.55.0 from an application that is based on libstdc++ on Mac.

Handling files - using boost::filesystem, boost::filesystem is not only a mature and often used library for file system which needs to be copied for each project into the web directory. You can use parent_path to get rid of the last element in the path, then filename to get the last element. Example: include boost/filesystem.hpp and iostream Example: include boost/filesystem.hpp and iostream

Comments
  • boost::filesystem::copy will copy directories or files alike. You can write a recursive algorithm that copies the directory and files within using that.
  • Ah. Thank you. I'm surprise that this isn't part of boost::filesystem. Also I couldn't find any documentation in the Boost library website that said in English what the function copy_directory actually does.
  • If you use C++ you, should use std::cerr instead of fprintf and stderr. And also, since this is Boost.Filesystem, you should use boost::path instead of std::string.
  • Thanks for the suggestions, I improved the function accordingly.
  • Note that you still have to be careful of what you're copying where. If you ran copyDir(boost::filesystem::path("."), boost::filesystem::path("test")), it will copy itself until it is terminated because the path length exceeds the limit, or you run out of disk space.
  • Thank you very much nijansen, that fits very neatly into what I am doing. I am copying a very well defined and known folder to another location so I don't need to worry about any special cases. I'm still surprised that copy_directory doesn't copy the contents or at least have an option to copy contents.
  • Instead of doing dest.string() + "/" you should use the / operator. dest / current.filename()
  • Many thanks Roi. I hope visitors notice your answer.
  • Note this was added to ISO C++ in C++ 17.
  • +1 for prefering exceptions over boolean return values. Also, relativePathStr can be computed using path.lexically_relative(sourceDir), which may be simpler to read than boost::replace_first.