Working with the File System

Getting the File Creation Time using std::filesystem

Is there a way to get the creation time of a file with std::filesystem?

Abstract art representing computer programming

As of C++20, the std::filesystem library does not provide a direct way to get the creation time of a file. However, you can get the last write time using the last_write_time() function.

File creation time may not be supported by all filesystems, which is one reason it is not part of the standard library. Here’s how you can get the last write 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 {
    auto ftime = fs::last_write_time(file_path);  
    auto sctp =
        ftime - fs::file_time_type::clock::now()
        + system_clock::now()

    std::time_t cftime =
    std::cout << "Last write time: "
      << std::asctime(std::localtime(&cftime));
  } catch (fs::filesystem_error &e) {
    std::cerr << e.what() << '\\n';
Last write time: Mon Jun 10 16:54:01 2024

In this example:

  • We define file_path as the path to the file whose last write time we want to retrieve.
  • We call fs::last_write_time() to get the file’s last write time, which returns a file_time_type.
  • We then convert file_time_type to a system_clock::time_point and finally to a std::time_t to print it in a human-readable format.

If you need to access the creation time and your operating system supports it, you might need to use platform-specific APIs.

For example, on Windows, you can use the GetFileTime() function from the Windows API to retrieve the creation time. Here’s a brief example using Windows API:

#include <windows.h>

#include <iostream>

void print_file_creation_time(
  const std::wstring &file_path) {
  HANDLE file_handle =

  if (file_handle == INVALID_HANDLE_VALUE) {
    std::cerr << "Failed to open file\n";

  FILETIME creation_time;
  if (GetFileTime(
  )) {
    FileTimeToSystemTime(&creation_time, &st);
    std::cout << "Creation time: "
      << st.wYear << '/'
      << st.wMonth << '/'
      << st.wDay << ' '
      << st.wHour << ':'
      << st.wMinute << ':'
      << st.wSecond << '\n';
  } else {
    std::cerr << "Failed to get file time\n";


int main() {
Creation time: 2023/6/10 23:10:33

This example uses the Windows API to open a file, retrieve its creation time, convert it to SYSTEMTIME, and print it.

Always consider the portability of your code. Using platform-specific APIs reduces portability but might be necessary for features not supported by the standard library.

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:

  • 124 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