名前空間
変種
操作

std::filesystem::copy_options

From cppreference.com
 
 
 
ヘッダー <filesystem> で定義
enum class copy_options {

    none = /* 不明 */,
    skip_existing = /* 未指定 */,
    overwrite_existing = /* 未指定 */,
    update_existing = /* 未指定 */,
    recursive = /* 未指定 */,
    copy_symlinks = /* 未指定 */,
    skip_symlinks = /* 未指定 */,
    directories_only = /* 未指定 */,
    create_symlinks = /* 未指定 */,
    create_hard_links = /* 未指定 */

};
(C++17以降)

この型は、copy() および copy_file() 関数の動作を制御するために利用可能なオプションを表します。

copy_optionsBitmaskType の要件を満たします(これは、ビット演算子 operator&operator|operator^operator~operator&=operator|=、および operator^= がこの型に対して定義されていることを意味します)。none は空のビットマスクを表し、他のすべての列挙子は個別のビットマスク要素を表します。

[編集] メンバー定数

以下のオプショングループの各グループにおいて、最大で 1 つのコピーオプションのみを指定できます。それ以外の場合は、コピー関数の動作は未定義です。

メンバー定数 意味
ファイルが既に存在する場合の copy_file() への影響を制御するオプション
なし エラーを報告します(デフォルトの動作)。
skip_existing エラーを報告せずに、既存のファイルを保持します。
overwrite_existing 既存のファイルを上書きします。
update_existing コピーされるファイルよりも古い場合にのみ、既存のファイルを上書きします。
copy() がサブディレクトリに与える影響を制御するオプション
なし サブディレクトリをスキップします(デフォルトの動作)。
recursive サブディレクトリとその内容を再帰的にコピーします。
copy() がシンボリックリンクに与える影響を制御するオプション
なし シンボリックリンクをたどります(デフォルトの動作)。
copy_symlinks シンボリックリンクを、それが指すファイルではなく、シンボリックリンクとしてコピーします。
skip_symlinks シンボリックリンクを無視します。
copy() が行うコピーの種類を制御するオプション
なし ファイルの内容をコピーします(デフォルトの動作)。
directories_only ディレクトリ構造をコピーしますが、ディレクトリ以外のファイルはコピーしません。
create_symlinks ファイルのコピーを作成する代わりに、元ファイルへのシンボリックリンクを作成します。注意:ソースパスは絶対パスである必要があります。ただし、宛先パスがカレントディレクトリにある場合は除きます。
create_hard_links ファイルのコピーを作成する代わりに、元ファイルと同じファイルを指すハードリンクを作成します。

[編集]

#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");
}

実行結果の例

.
├── sandbox
│   ├── dir
│   │   └── subdir
│   ├── dir2
│   ├── file1.txt
│   └── file2.txt
└── sandbox_copy
    ├── dir
    │   └── subdir
    └── dir2
 
8 directories, 2 files

[編集] 関連項目

(C++17)
ファイルまたはディレクトリをコピーする
(関数) [編集]
(C++17)
ファイルの内容をコピーする
(関数) [編集]
English 日本語 中文(简体) 中文(繁體)