Getting the File Creation Time using std::filesystem

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

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 =
      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 << "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 std::filesystem::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 =
    CreateFileW(
      file_path.c_str(),
      GENERIC_READ,
      FILE_SHARE_READ,
      nullptr,
      OPEN_EXISTING,
      FILE_ATTRIBUTE_NORMAL,
      nullptr
    );

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

  FILETIME creation_time;
  if (GetFileTime(
    file_handle,
    &creation_time,
    nullptr,
    nullptr
  )) {
    SYSTEMTIME st;
    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";
  }

  CloseHandle(file_handle);
}

int main() {
  print_file_creation_time(L"c:\\test\\hello.txt");  
}
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.

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?
Watching for Directory Changes using std::filesystem
Can I watch a directory for changes using std::filesystem?
Setting Last Modified Time using std::filesystem
How do I set the last modified time of a file 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