I am trying to copy a folder to another folder using std::filesystem::copy(), so far it only copies the files and folders within the folder I'm trying to move over, instead of the folder itself. Any ideas why?
I know this could be done manually by creating a new directory using std::filesystem::create_directory(), but it won't carry over the security info and permissions from the original folder.
EDIT:
path = C:\Users\Test\Folder
nPath = C:\Users\Test\Movehere
boolean CopyToPath(std::string path, std::string nPath) {
if (fs::exists(path) && fs::exists(nPath)) {
try {
//path = Folder To Copy, nPath = Destination
fs::copy(path, nPath, fs::copy_options::overwrite_existing | fs::copy_options::recursive);
return true;
}
catch (fs::filesystem_error e) {
std::cout << e.what() << "\n";
return false;
}
}
else
return false;
}
CodePudding user response:
This is expected behavior, as documented at https://en.cppreference.com/w/cpp/filesystem/copy:
- Otherwise, if
fromis a directory and eitheroptionshascopy_options::recursiveor iscopy_options::none,
- If
todoes not exist, first executescreate_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 byfor (const std::filesystem::directory_entry& x : std::filesystem::directory_iterator(from))and for each directory entry, recursively callscopy(x.path(), to/x.path().filename(), options | in-recursive-copy), where in-recursive-copy is a special bit that has no other effect when set inoptions. (The sole purpose of setting this bit is to prevent recursive copying subdirectories ifoptionsiscopy_options::none.)
In other words, copy() does not copy the source directory itself, only the contents of the directory.
If your goal is to make a copy of Folder itself inside of Movehere, ie C:\Users\Test\Movehere\Folder, then you will have to extract Folder from the source directory and append it to the target path, eg:
fs::path src = "C:\\Users\\Test\\Folder";
fs::path dest = "C:\\Users\\Test\\Movehere";
dest /= src.filename();
fs::create_directory(dest, src);
// only because CopyToPath() requires this due
// to its use of fs::exists(), instead of letting
// fs::copy() create it...
CopyToPath(src, dest);
