C++ 如何使用 Boost Filesystem 复制目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8593608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:50:51  来源:igfitidea点击:

How can I copy a directory using Boost Filesystem

c++boostfilesystemsdirectorycopy

提问by Ant

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.

如何使用 Boost Filesystem 复制目录?我试过 boost::filesystem::copy_directory() 但它只创建目标目录而不复制内容。

回答by nijansen

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("/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)

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

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 falseif 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.

重构了与评论相关的功能。此外,该函数现在返回成功结果。false如果不满足给定目录或源目录中任何目录的要求,它将返回,但如果无法复制单个文件,则不会返回。

回答by Roi Danton

Since C++17 you don't need boost for this operation anymore as filesystem has been added to the standard.

从 C++17 开始,由于文件系统已添加到标准中,因此您不再需要提升此操作。

Use std::filesystem::copy

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.

另见std::filesystem::copy_options

回答by Doineann

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.

我认为这个版本是@nijansen 答案的改进版本。它还支持相对的源和/或目标目录。

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_iteratorand boost::replace_firstto 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).

主要区别在于异常而不是返回值、使用recursive_directory_iteratorboost::replace_first去除迭代器路径的公共部分,以及依赖于boost::filesystem::copy()对不同文件类型(例如,保留符号链接)做正确的事情。

回答by GuidedHacking

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.

这是我使用的基于 Doineann 代码的非 Boost 版本。我正在使用 std::filesystem 但不能使用简单的,fs::copy(src, dst, fs::copy_options::recursive);因为我想过滤哪些文件是通过循环内的文件扩展名复制的。

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

relstr.erase(0, std::wstring(src).size());是其他答案中使用的 boost::replace_first() 调用的无 Boost 替代品