3

I'm looking for simple way to copy folders recursively without losing files and folders timestamp. I find std::filesystem::copy so simple and easy to use, but it set files modification times to now.

alihardan
  • 183
  • 1
  • 14

1 Answers1

8

You can read the modification time for each file you copy, into a temporary variable, and after having copied it to its destination, modify the metadata on the file to revert back to the older modified time.

Something like this:

#include <chrono>
#include <filesystem>

namespace fs = std::filesystem;

int main()
{
    fs::path p = ... // your file

    const auto modify_time = fs::last_write_time(p);
 
    // Copy to new location

    fs::last_write_time(p_new_location, modify_time);
}

Mansoor
  • 2,357
  • 1
  • 17
  • 27