std::filesy...copy_options(3)
β¨ NAME β¨
okay bestie, we're in the C++ Standard Libary manual (yes, that's literally the spelling on the page, not me π), and this is the page for std::filesystem::copy_options β aka std::filesystem::copy_options. iconic name, honestly π
π¦ Synopsis π¦
Defined in header <filesystem>:
Defined in header <filesystem>
enum class copy_options {
none = /* unspecified */,
skip_existing = /* unspecified */,
overwrite_existing = /* unspecified */,
update_existing = /* unspecified */,
recursive = /* unspecified */, (since C++17)
copy_symlinks = /* unspecified */,
skip_symlinks = /* unspecified */,
directories_only = /* unspecified */,
create_symlinks = /* unspecified */,
create_hard_links = /* unspecified */
};
So this type is literally the options menu for controlling the behavior of the copy() and copy_file() function. very main character energy for an enum, ngl π
copy_options satisfies the requirements of BitmaskType β which means the bitwise operators operator&, operator|, operator^, operator~, operator&=, operator|=, and operator^= are defined for this type. none represents the empty bitmask; every other enumerator represents a distinct bitmask element. slay, bitwise bestie β¨
π Member constants π
okay so listen: at most one copy option in each of the following options groups may be present, otherwise the behavior of the copy functions is undefined. no cap, do not mix and match inside a group π
π options controlling copy_file() when the file already exists π
noneβ Report an error (default behavior).skip_existingβ Keep the existing file, without reporting an error.overwrite_existingβ Replace the existing file.update_existingβ Replace the existing file only if it is older than the file being copied.
πΈ options controlling the effects of copy() on subdirectories πΈ
noneβ Skip subdirectories (default behavior).recursiveβ Recursively copy subdirectories and their content.
π¦ options controlling the effects of copy() on symbolic links π¦
noneβ Follow symlinks (default behavior).copy_symlinksβ Copy symlinks as symlinks, not as the files they point to.skip_symlinksβ Ignore symlinks.
π options controlling the kind of copying copy() does π
noneβ Copy file content (default behavior).directories_onlyβ Copy the directory structure, but do not copy any non-directory files.create_symlinksβ Instead of creating copies of files, create symlinks pointing to the originals. Note: the source path must be an absolute path unless the destination path is in the current directory.create_hard_linksβ Instead of creating copies of files, create hardlinks that resolve to the same files as the originals.
π Example π
// Run this code
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::filesystem;
int main()
{
fs::create_directories("sandbox/dir/subdir");
std::ofstream("sandbox/file1.txt").put('a');
fs::copy("sandbox/file1.txt", "sandbox/file2.txt"); // copy file
fs::copy("sandbox/dir", "sandbox/dir2"); // copy directory (non-recursive)
const auto copyOptions = fs::copy_options::update_existing
| fs::copy_options::recursive
| fs::copy_options::directories_only
;
fs::copy("sandbox", "sandbox_copy", copyOptions);
static_cast<void>(std::system("tree"));
fs::remove_all("sandbox");
fs::remove_all("sandbox_copy");
}
π Possible output π
.
βββ sandbox
β βββ dir
β β βββ subdir
β βββ dir2
β βββ file1.txt
β βββ file2.txt
βββ sandbox_copy
βββ dir
β βββ subdir
βββ dir2
8 directories, 2 files
π₯ See also π₯
copyβ (C++17) copies files or directories (function)copy_fileβ (C++17) copies file contents (function)
π source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::filesy...copy_options(3) β and that's the whole manual page, bestie. periodt. π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms