Get File Creation Date in C

To get the file creation date in C, use system-specific functions; on Unix-like systems, you can use the stat function (which may return the file’s status change time instead of the true creation time), and on Windows, you can use the Windows API functions such as GetFileTime to obtain the creation date.


Example 1: Using stat Function (Unix-like Systems)

This example demonstrates how to retrieve the file creation date (or the status change time on systems where creation time is unavailable) using the stat function. We will access the attributes of a file named “example.txt” and print its time.

main.c

</>
Copy
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>

int main() {
    struct stat attr;
    // Retrieve file attributes of "example.txt"
    if (stat("example.txt", &attr) == 0) {
        // Print the file creation (or status change) time
        printf("File creation (or status change) time: %s", ctime(&attr.st_ctime));
    } else {
        perror("stat");
    }
    return 0;
}

Explanation:

  1. struct stat attr; declares a structure to store the file attributes.
  2. The stat() function is used with the filename “example.txt” to fill the attr structure with file information.
  3. If stat() succeeds, attr.st_ctime contains the file’s status change time (which may represent the creation time on some systems).
  4. The ctime() function converts the time stored in attr.st_ctime into a human-readable string, which is then printed using printf().
  5. If the file cannot be accessed, perror() displays an appropriate error message.

Output:

File creation (or status change) time: Wed Mar 10 14:23:45 2021

Example 2: Using Windows API (GetFileTime)

This example demonstrates how to retrieve the file creation date on Windows by using the Windows API. We will open the file “example.txt”, retrieve its creation time using GetFileTime, convert it to local time, and then print the formatted date.

main.c

</>
Copy
#include <windows.h>
#include <stdio.h>

int main() {
    // Open the file "example.txt" for reading
    HANDLE hFile = CreateFile("example.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
    if (hFile == INVALID_HANDLE_VALUE) {
        printf("Could not open file.\n");
        return 1;
    }
    FILETIME ftCreate;
    // Retrieve the file creation time
    if (!GetFileTime(hFile, &ftCreate, NULL, NULL)) {
        printf("Could not get file time.\n");
        CloseHandle(hFile);
        return 1;
    }
    SYSTEMTIME stUTC, stLocal;
    // Convert the FILETIME structure to SYSTEMTIME in UTC
    FileTimeToSystemTime(&ftCreate, &stUTC);
    // Convert UTC time to local time
    SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
    // Print the file creation date in a readable format
    printf("File creation date: %02d/%02d/%d  %02d:%02d\n", stLocal.wMonth, stLocal.wDay, stLocal.wYear, stLocal.wHour, stLocal.wMinute);
    CloseHandle(hFile);
    return 0;
}

Explanation:

  1. CreateFile() opens “example.txt” with read access and returns a handle (hFile).
  2. GetFileTime() retrieves the creation time of the file and stores it in the FILETIME variable ftCreate.
  3. FileTimeToSystemTime() converts the FILETIME value to a SYSTEMTIME structure (stUTC) in Coordinated Universal Time (UTC).
  4. SystemTimeToTzSpecificLocalTime() converts the UTC time (stUTC) to local time (stLocal).
  5. printf() formats and prints the local creation date and time.
  6. CloseHandle() closes the file handle to release system resources.

Output:

File creation date: 03/10/2021  14:23

Conclusion

In this tutorial, we explored two approaches for obtaining the file creation date in C:

  1. Example 1: Uses the stat function on Unix-like systems to retrieve file attributes, with st_ctime representing the status change time (which may be the creation time on some systems).
  2. Example 2: Uses Windows API functions (CreateFile, GetFileTime, etc.) to retrieve and convert the file creation date on Windows.