Setting Last Modified Time using std::filesystem

How do I set the last modified time of a file using std::filesystem?

You can set the last modified time of a file using the last_write_time() function from the std::filesystem library. This function allows you to set the last write time to a specified file_time_type.

Here's an example demonstrating how to set the last modified time:

#include <chrono>
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;

int main() {
  using namespace std::chrono;
  fs::path file_path{R"(c:\test\hello.txt)"};  
  try {
    // Create a time_point representing the new time
    auto new_time =
      file_clock::now() - hours(24);

    // Set the new last write time
    fs::last_write_time(file_path, new_time);  

    // Verify the change
    auto ftime = fs::last_write_time(file_path);
    auto sctp =
      time_point_cast<system_clock::duration>(
        ftime - fs::file_time_type::clock::now() +
        system_clock::now());

    std::time_t cftime =
      system_clock::to_time_t(sctp);
    std::cout << "New last write time: "
      << std::asctime(std::localtime(&cftime));
  } catch (fs::filesystem_error &e) {
    std::cerr << e.what() << '\n';
  }
}
New last write time: Sun Jun 09 23:10:33 2023

In this example:

  • We define file_path as the path to the file we want to modify.
  • We create a time_point representing the new time we want to set, which is 24 hours before the current time.
  • We call std::filesystem::last_write_time() with the file path and the new time to set the last modified time.
  • We then retrieve and print the last modified time to verify the change.

The std::chrono library is used to handle time points. The system_clock::now() function returns the current time, and we subtract 24 hours to set the new time. You can adjust the new_time calculation as needed to set the last modified time to a specific date and time.

Remember to handle exceptions, as setting the last write time may fail if the file does not exist or if there are permission issues. The try block and catch block handle any std::filesystem::filesystem_error exceptions and print the error message.

This method allows you to programmatically update the last modified time of a file, which can be useful for various applications, such as synchronizing files or simulating file updates.

Working with the File System

Create, delete, move, and navigate through directories and files using the std::filesystem library.

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

List Files in Directory
How do I list all files in a directory using std::filesystem?
Working with Symbolic Links
How can I work with symbolic links using std::filesystem?
Overwriting an Existing Directory
What happens if I try to create a directory that already exists?
Getting the File Creation Time using std::filesystem
Is there a way to get the creation time of a file with std::filesystem?
Watching for Directory Changes using std::filesystem
Can I watch a directory for changes using std::filesystem?
Compresssing Files using std::filesystem
Can I use std::filesystem to compress files?
Copying Directory Structures using std::filesystem
How do I copy a directory structure without copying files using std::filesystem?
std::filesystem Thread Safety
Is std::filesystem safe to use in multithreaded contexts?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant