c++windowswinapimfc

Win32 copy files without replacement


I am currently using this answer to copy a directory in my code: https://stackoverflow.com/a/11483739/14296133

I ran into an issue that this copies and replaces all the files with the same name. For example, if my target directory already has a file "test.txt" and my source directory has a different "test.txt" in it, the new contents will be copied over, but I want the old ones to stay. This means I only want to copy files that are missing in the target dir.

I looked at Microsoft's documentation and couldn't find any flag that copies files using SHFileOperation without replacing the existing ones. My question is whether this flag exists, and if not, what is the alternative? Preferably using the SHFileOperation interface.


Solution

  • I managed to get C++17 to work in my project, so here's the C++17 rewrite of the same function:

    std::error_code CopyDirTo(const std::wstring& source_folder, const std::wstring& target_folder) {
      namespace fs = std::filesystem;
      std::error_code error_code;
      const auto copy_options = fs::copy_options::recursive | fs::copy_options::skip_existing;
      fs::copy(source_folder, target_folder, copy_options, error_code);
      return error_code;
    }