Home > database >  std::filesystem::copy() only copies files in folder
std::filesystem::copy() only copies files in folder

Time:10-19

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 from is a directory and either options has copy_options::recursive or is copy_options::none,
    • If to does not exist, first executes create_directory(to, from) (creates the new directory with a copy of the old directory's attributes)
    • Then, whether to already existed or was just created, iterates over the files contained in from as if by for (const std::filesystem::directory_entry& x : std::filesystem::directory_iterator(from)) and for each directory entry, recursively calls copy(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 in options. (The sole purpose of setting this bit is to prevent recursive copying subdirectories if options is copy_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);
  • Related