Working with the File System

Setting Last Modified Time using std::filesystem

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

Abstract art representing computer programming

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 fs::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 fs::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.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved