我如何使用Boost文件系统复制目录 [英] How can I copy a directory using Boost Filesystem

查看:354
本文介绍了我如何使用Boost文件系统复制目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以复制使用升压文件系统的目录?
我已经试过的boost ::文件系统:: copy_directory(),但只有创建目标目录,并不会复制的内容。


解决方案

 布尔copydir命令(
    提高::文件系统::路径常量和放大器;资源,
    提高::文件系统::路径常量和放大器;目的地

{
    命名空间FS =的boost ::文件系统;
    尝试
    {
        //检查函数调用是否有效
        如果(
            !FS ::存在(源)||
            !FS :: is_directory(源)
        )
        {
            的std :: CERR<< 源目录<< source.string()
                << 不存在或不是一个目录。 <<的'\\ n'
            ;
            返回false;
        }
        如果(FS ::存在的(目标))
        {
            的std :: CERR<< 目标目录<< destination.string()
                << 已经存在。 <<的'\\ n'
            ;
            返回false;
        }
        //创建目标目录
        如果(!FS :: create_directory(目标))
        {
            的std :: CERR<< 无法创建目标目录
                << destination.string()&所述;&下;的'\\ n'
            ;
            返回false;
        }
    }
    赶上(FS :: filesystem_error常量与评估)
    {
        的std :: CERR<< e.what()&所述;&下;的'\\ n';
        返回false;
    }
    //通过源目录遍历
    对于(
        FS :: directory_iterator文件(源);
        文件= FS :: directory_iterator()!; ++文件
    )
    {
        尝试
        {
            飞秒::路径电流(文件 - >路径());
            如果(FS :: is_directory(电流))
            {
                // found目录:递归
                如果(
                    !copydir命令(
                        当前,
                        目的地/ current.filename()
                    )
                )
                {
                    返回false;
                }
            }
            其他
            {
                //找到的文件:复制
                FS :: COPY_FILE(
                    当前,
                    目的地/ current.filename()
                );
            }
        }
        赶上(FS :: filesystem_error常量与评估)
        {
            的std :: CERR<< e.what()&所述;&下;的'\\ n';
        }
    }
    返回true;
}

用法:

copydir命令(升压::文件系统::路径(/家庭/ nijansen /测试),提振::文件系统::路径(/家庭/ nijansen / test_copy)); (UNIX)

copydir命令(升压::文件系统::路径(C:\\\\ \\\\用户\\\\ nijansen测试),提振::文件系统::路径(C:\\\\ \\\\的用户nijansen \\\\ TEST2)); (视窗)

据我看到的,可能发生的最糟糕的是,什么也不会发生,但是我什么也不答应!使用您自己的风险。

请注意,您复制到该目录必须不存在。如果您正试图复制的目录中的目录无法读取(思权限管理),它们将被跳过,而其他的人仍然应该被复制。

更新

重构功能各自的评论。此外,该函数现在返回成功的结果。它会返回如果给定的目录或源目录中的任何目录的要求没有得到满足,但如果单个文件不能被复制。

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文件系统复制目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆