how to perform boost::filesystem copy_file with overwrite

Dani van der Meer picture Dani van der Meer · May 6, 2009 · Viewed 45.4k times · Source

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.

Answer

anno picture anno · Apr 27, 2010

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