名稱空間
變體
操作

std::filesystem::copy_options

來自 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_options 滿足 BitmaskType 的要求(這意味著位運算子 operator&operator|operator^operator~operator&=operator|=operator^= 為此型別定義)。none 表示空位掩碼;其他每個列舉器都表示一個不同的位掩碼元素。

[編輯] 成員常量

以下每個選項組中最多隻能存在一個複製選項,否則複製函式的行為是未定義的。

成員常量 含義
當檔案已存在時控制 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)
複製檔案內容
(函式) [編輯]