Search code examples
c++windowsboostboost-filesystem

how to perform boost::filesystem copy_file with overwrite


The Windows API function CopyFile has an argument BOOL bFailIfExists that allows you to control whether or not you want to overwrite the target file if it exists.

The boost::filesystem copy_file function has no such argument, and will fail if the target file exists. Is there an elegant way to use the boost copy_file function and overwrite the target file? Or is it better to simply use the Windows API? My current target platform is Windows, but I prefer to use STL and boost where possible to keep my code platform independent.

Thank you.


Solution

  • There's a third enum argument to copy_file, boost::filesystem::copy_option::overwrite_if_exists

    copy_file(source_path, destination_path, copy_option::overwrite_if_exists);
    

    https://www.boost.org/doc/libs/1_75_0/libs/filesystem/doc/reference.html