user7600596
user7600596

Reputation:

Using more than one copy option for filesystem::copy in C++

I am using filesystem to copy a directory. I want the directory to be copied recursively and to overwrite any existing files.

I use the following code to copy the directory and it works. However, I can only set recursive OR overwrite_existing for copy_options, not both.

filesystem::copy(pathInput, pathOutput, filesystem::copy_options::recursive);

Is there a way that I can use multiple options with the copy function?

I am using this site as a reference for the options but it does not say anything about using more than one option at the same time.

Upvotes: 1

Views: 779

Answers (2)

Prodigle
Prodigle

Reputation: 1797

They're bit flags so

filesystem::copy(pathInput, pathOutput, filesystem::copy_options::recursive | filesystem::copy_options::overwrite_existing)

To add additional detail, on a lower level each flag is a bit (on/off) in an integer so...

01 = overwrite 10 = recursive 11 = both 00 = none

the | operator is a binary or which equals true if either bit (or both) is true so:

01 | 10 = 11

00 | 11 = 11

11 | 11 = 11

00 | 00 = 00

Upvotes: 4

Sam P
Sam P

Reputation: 305

Using the reference site you gave it says right above the constant definitions that

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

So you can just do what everyone else says and or the two values together since they are from different options groups.

Upvotes: 2

Related Questions