How to recursively copy files and directories(如何递归复制文件和目录)
问题描述
使用C++,是否可以将文件和目录从一个路径递归复制到另一个路径
- 而不必使用任何附加库?
- 并且具有独立于平台的功能?
考虑以下文件系统
src/fileInRoot
src/sub_directory/
src/sub_directory/fileInSubdir
我要复制
- 所有文件和目录或
- 某些文件和目录
从src到另一个目录target。
我已经创建了一个新问题,因为我找到的问题是特定于平台的,不包括筛选:
- C++ Copy directory recursive under unix(Unix)
- Copy directory content(Linux)
- C/C++ Copy file with automatic recursive folder/directory creation(Winapi)
推荐答案
可以,只使用STD C++.就可以复制完整的目录结构从C++17开始,其std::filesystem包括std::filesystem::copy。
- 可以使用
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();
}
}
- 要使用过滤复制文件的某个子集,可以使用
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_optionsAs参数传递给CopyRecursive()以获得更大的灵活性。
上面使用的std::filesystem中的一些函数列表:
relative()减去路径和注意符号链接 (使用path::lexically_relative()和weakly_canonical())create_directories()为给定路径中尚不存在的每个元素创建目录current_path()返回(或更改)当前工作目录的绝对路径path::stem()返回不带最终扩展名的文件名path::generic_string()给出带有平台无关目录分隔符的窄字符串/
对于生产代码,我建议将错误处理从实用程序函数中提取出来。对于错误处理,std::filesystem提供两种方法:
- 异常,
std::exception/std::filesystem::filesystem_error - 和错误代码
std::error_code。
还应考虑,
std::filesystem可能not be available on all platforms
如果实现无法访问分层文件系统,或者如果它不提供必要的功能,则文件系统库功能可能不可用。如果基础文件系统不支持某些功能(例如,FAT文件系统缺少符号链接并禁止多个硬链接),则这些功能可能不可用。在这些情况下,必须报告错误。
这篇关于如何递归复制文件和目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何递归复制文件和目录
基础教程推荐
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- CString 到 char* 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
