std::filesystem::copy
From cppreference.com
< cpp | filesystem
| Defined in header <filesystem>
|
||
| void copy( const std::filesystem::path& from, const std::filesystem::path& to ); |
(1) | (since C++17) |
| void copy( const std::filesystem::path& from, const std::filesystem::path& to, |
(2) | (since C++17) |
Copies files and directories, with a variety of options
1) The default, equivalent to (2) with
copy_options::none used as options2) Copies the file or directory
from to file or directory to, using the copy options indicated by options. The behavior is undefined if there is more than one option in any of the copy_options option group present in options (even in the copy_file group, which is not relevant to copy)The behavior is as follows:
- First, before doing anything else, obtains type and permissions of
fromby no more than single call to std::filesystem::status (or, ifcopy_options::skip_symlinksorcopy_options::create_symlinksare present inoptions, by a call to std::filesystem::symlink_status). - If necessary, obtains the status of
tothe same way, by no more than a single status or symlink_status call. - If
fromdoes not exist, reports an error. - If
fromandtoare the same file as determined by std::filesystem::equivalent, reports an error - If either
fromortois not a regular file, a directory, or a symlink, as determined by std::filesystem::is_other, reports an error - If
fromis a directory, buttois a regular file, reports an error - If
fromis a symbolic link, then
- If
copy_options::skip_symlinkis present inoptions, does nothing. - Otherwise, if
todoes not exist andcopy_options::copy_symlinksis present inoptions, then behaves as if copy_symlink(from, to) - Otherwise, reports an error
- If
- Otherwise, if
fromis a regular file, then
- If
copy_options::directories_onlyis present inoptions, does nothing - Otherwise, if
copy_options::create_symlinksis present inoptions, creates a symlink toto. Note:frommust be an absolute path unlesstois in the current directory. - Otherwise, if
copy_options::create_hard_linksis present inoptions, creates a hard link toto - Otherwise, if
tois a directory, then behaves as if copy_file(from, to/from.filename(), options) (creates a copy offromas a file in the directoryto) - Otherwise, behaves as if copy_file(from, to, options) (copies the file)
- If
- Otherwise, if
fromis a directory and eitheroptionshascopy_options::recursiveor iscopy_options::none,
- If
todoes not exist, first executes create_directory(to, from) (creates the new directory with a copy of the old directory's attributes) - Then, whether
toalready existed or was just created, iterates over the files contained infromas if by for (std::filesystem::const_directory_entry& x : std::filesystem::directory_iterator(from)) and for each directory entry, recursively calls copy(x.path(), to/x.path().filename(), options | unspecified), where unspecified is some unspecified set of copy options.
- If
- Otherwise does nothing
Contents |
[edit] Parameters
| from | - | path to the source file, directory, or symlink |
| to | - | path to the target file, directory, or symlink |
| ec | - | out-parameter for error reporting in the non-throwing overload |
[edit] Return value
(none)
[edit] Exceptions
The overload that does not take a std::error_code& parameter throws filesystem_error on underlying OS API errors, constructed withfrom as the first argument, to as the second argument, and the OS error code as the error code argument. std::bad_alloc may be thrown if memory allocation fails. The overload taking a std::error_code& parameter sets it to the OS API error code if an OS API call fails, and executes ec.clear() if no errors occur. This overload has noexcept specification:
noexcept
[edit] Notes
The default behavior when copying directories is the non-recursive copy: the files are copied, but not the subdirectories:
// Given // /dir1 contains /dir1/file1, /dir1/file2, /dir1/dir2 // and /dir1/dir2 contains /dir1/dir2/file3 // After std::filesystem::copy("/dir1", "/dir3"); // /dir3 is created (with the attributes of /dir1) // /dir1/file1 is copied to /dir3/file1 // /dir1/file2 is copied to /dir3/file2
While with copy_options::recursive, the subdirectories are also copied, with their content, recursively.
// ...but after std::filesystem::copy("/dir1", "/dir3", std::filesystem::copy_options::recursive); // /dir3 is created (with the attributes of /dir1) // /dir1/file1 is copied to /dir3/file1 // /dir1/file2 is copied to /dir3/file2 // /dir3/dir2 is created (with the attributes of /dir1/dir2) // /dir1/dir2/file3 is copied to /dir3/dir2/file3
[edit] Example
Run this code
#include <iostream> #include <fstream> #include <filesystem> 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) // sandbox holds 2 files and 2 directories, one of which has a subdirectory // sandbox/file1.txt // sandbox/file2.txt // sandbox/dir2 // sandbox/dir // sandbox/dir/subdir fs::copy("sandbox", "sandbox/copy", fs::copy_options::recursive); // sandbox/copy holds copies of the above files and subdirectories fs::remove_all("sandbox"); }
[edit] See also
| (C++17) |
specifies semantics of copy operations (enum) |
| (C++17) |
copies a symbolic link (function) |
| (C++17) |
copies file contents (function) |