gpt4 book ai didi

c++ - 如何递归复制文件和目录

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:15:32 29 4
gpt4 key购买 nike

使用 C++,是否可以递归地将文件和目录从一个路径复制到另一个路径

  • 无需使用任何额外的库?
  • 并具有独立于平台的功能?

考虑以下文件系统

src/fileInRoot
src/sub_directory/
src/sub_directory/fileInSubdir

我要复制

  1. 所有文件和目录或
  2. 某些文件和目录

src到另一个目录target


我创建了一个新问题,因为我发现的问题是特定于平台的,不包括过滤:

最佳答案

是的,可以使用标准 C++ 复制完整的目录结构 ... 从 C++17 及其 std::filesystem 开始其中包括std::filesystem::copy .

  1. 可以使用 copy_options::recursive 复制所有文件:
// Recursively copies all files and folders from src to target and overwrites existing files in target.
void CopyRecursive(const fs::path& src, const fs::path& target) noexcept
{
try
{
fs::copy(src, target, fs::copy_options::overwrite_existing | fs::copy_options::recursive);
}
catch (std::exception& e)
{
std::cout << e.what();
}
}
  1. 要使用过滤器复制特定的文件子集,recursive_directory_iterator可以利用:
// Recursively copies those files and folders from src to target which matches
// predicate, and overwrites existing files in target.
void CopyRecursive(const fs::path& src, const fs::path& target,
const std::function<bool(fs::path)>& predicate /* or use template */) noexcept
{
try
{
for (const auto& dirEntry : fs::recursive_directory_iterator(src))
{
const auto& p = dirEntry.path();
if (predicate(p))
{
// Create path in target, if not existing.
const auto relativeSrc = fs::relative(p, src);
const auto targetParentPath = target / relativeSrc.parent_path();
fs::create_directories(targetParentPath);

// Copy to the targetParentPath which we just created.
fs::copy(p, targetParentPath, fs::copy_options::overwrite_existing);
}
}
}
catch (std::exception& e)
{
std::cout << e.what();
}
}

像调用第二种方法时

#include <filesystem>
#include <iostream>
#include <functional>
namespace fs = std::filesystem;

int main()
{
const auto root = fs::current_path();
const auto src = root / "src";
const auto target = root / "target";

// Copy only those files which contain "Sub" in their stem.
const auto filter = [](const fs::path& p) -> bool
{
return p.stem().generic_string().find("Sub") != std::string::npos;
};
CopyRecursive(src, target, filter);
}

并且给定的文件系统在进程的工作目录中,则结果为

target/sub_directory/
target/sub_directory/fileInSubdir

您还可以将 copy_options 作为参数传递给 CopyRecursive(),以获得更大的灵 active 。


上面使用的 std::filesystem 中的一些函数列表:


对于生产代码,我建议从实用函数中提取错误处理。对于错误处理,std::filesystem 提供了两种方法:

  1. 异常 std::exception/std::filesystem::filesystem_error
  2. 和错误代码 std::error_code .

还要考虑到,std::filesystem 可能 not be available on all platforms

The filesystem library facilities may be unavailable if a hierarchical file system is not accessible to the implementation, or if it does not provide the necessary capabilities. Some features may not be available if they are not supported by the underlying file system (e.g. the FAT filesystem lacks symbolic links and forbids multiple hardlinks). In those cases, errors must be reported.

关于c++ - 如何递归复制文件和目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51431425/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com