Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/filesystem/copy options"

From cppreference.com
m (~wiki-template)
m (fmt, .)
 
Line 2: Line 2:
 
{{cpp/filesystem/navbar}}
 
{{cpp/filesystem/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | filesystem}}
+
{{dcl header|filesystem}}
{{dcl | since=c++17 | 1=
+
{{dcl|since=c++17|1=
 
enum class copy_options {
 
enum class copy_options {
 
     none = /* unspecified */,
 
     none = /* unspecified */,
Line 25: Line 25:
 
===Member constants===
 
===Member constants===
 
At most one copy option in each of the following options groups may be present, otherwise the behavior of the copy functions is undefined.
 
At most one copy option in each of the following options groups may be present, otherwise the behavior of the copy functions is undefined.
{| class="wikitable" style="text-align:left; font-size:85%; max-width:90em;"
+
{|class="wikitable" style="text-align:left; font-size:85%; max-width:90em;"
 
|-
 
|-
! Member constant
+
!Member constant
! Meaning
+
!Meaning
 
|-
 
|-
 
!colspan=2|options controlling {{ltt|cpp/filesystem/copy_file|copy_file()}} when the file already exists
 
!colspan=2|options controlling {{ltt|cpp/filesystem/copy_file|copy_file()}} when the file already exists
 
|-
 
|-
| {{tt|none}}  
+
|{{tt|none}}  
| Report an error (default behavior)
+
|Report an error (default behavior).
 
|-
 
|-
| {{tt|skip_existing}}  
+
|{{tt|skip_existing}}  
| Keep the existing file, without reporting an error.
+
|Keep the existing file, without reporting an error.
 
|-
 
|-
| {{tt|overwrite_existing}}
+
|{{tt|overwrite_existing}}
| Replace the existing file
+
|Replace the existing file.
 
|-
 
|-
| {{tt|update_existing}}
+
|{{tt|update_existing}}
| Replace the existing file only if it is older than the file being copied
+
|Replace the existing file only if it is older than the file being copied.
 
|-
 
|-
 
!colspan=2|options controlling the effects of {{ltt|cpp/filesystem/copy|copy()}} on subdirectories
 
!colspan=2|options controlling the effects of {{ltt|cpp/filesystem/copy|copy()}} on subdirectories
 
|-
 
|-
| {{tt|none}}
+
|{{tt|none}}
| Skip subdirectories (default behavior)
+
|Skip subdirectories (default behavior).
 
|-
 
|-
| {{tt|recursive}}
+
|{{tt|recursive}}
| Recursively copy subdirectories and their content
+
|Recursively copy subdirectories and their content.
 
|-
 
|-
 
!colspan=2|options controlling the effects of {{ltt|cpp/filesystem/copy|copy()}} on symbolic links
 
!colspan=2|options controlling the effects of {{ltt|cpp/filesystem/copy|copy()}} on symbolic links
 
|-
 
|-
| {{tt|none}}
+
|{{tt|none}}
| Follow symlinks (default behavior)
+
|Follow symlinks (default behavior).
 
|-
 
|-
| {{tt|copy_symlinks}}
+
|{{tt|copy_symlinks}}
| Copy symlinks as symlinks, not as the files they point to
+
|Copy symlinks as symlinks, not as the files they point to.
 
|-
 
|-
| {{tt|skip_symlinks}}
+
|{{tt|skip_symlinks}}
| Ignore symlinks
+
|Ignore symlinks.
 
|-
 
|-
 
!colspan=2|options controlling the kind of copying {{ltt|cpp/filesystem/copy|copy()}} does
 
!colspan=2|options controlling the kind of copying {{ltt|cpp/filesystem/copy|copy()}} does
 
|-
 
|-
| {{tt|none}}
+
|{{tt|none}}
| Copy file content (default behavior)
+
|Copy file content (default behavior).
 
|-
 
|-
| {{tt|directories_only}}
+
|{{tt|directories_only}}
| Copy the directory structure, but do not copy any non-directory files
+
|Copy the directory structure, but do not copy any non-directory files.
 
|-
 
|-
| {{tt|create_symlinks}}
+
|{{tt|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.
+
|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.
 
|-
 
|-
| {{tt|create_hard_links}}
+
|{{tt|create_hard_links}}
| Instead of creating copies of files, create hardlinks that resolve to the same files as the originals
+
|Instead of creating copies of files, create hardlinks that resolve to the same files as the originals.
 
|}
 
|}
  
Line 83: Line 83:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/filesystem/dsc copy}}
+
{{dsc inc|cpp/filesystem/dsc copy}}
{{dsc inc | cpp/filesystem/dsc copy_file}}
+
{{dsc inc|cpp/filesystem/dsc copy_file}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|es|ja|zh}}
 
{{langlinks|es|ja|zh}}

Latest revision as of 01:42, 4 September 2023

 
 
 
Defined in header <filesystem>
enum class copy_options {

    none = /* unspecified */,
    skip_existing = /* unspecified */,
    overwrite_existing = /* unspecified */,
    update_existing = /* unspecified */,
    recursive = /* unspecified */,
    copy_symlinks = /* unspecified */,
    skip_symlinks = /* unspecified */,
    directories_only = /* unspecified */,
    create_symlinks = /* unspecified */,
    create_hard_links = /* unspecified */

};
(since C++17)

This type represents available options that control the behavior of the copy() and copy_file() function.

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.

[edit] Member constants

At most one copy option in each of the following options groups may be present, otherwise the behavior of the copy functions is undefined.

Member constant Meaning
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.

[edit] Example

#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

[edit] See also

(C++17)
copies files or directories
(function) [edit]
(C++17)
copies file contents
(function) [edit]