Windows File Creation Time Setter
win32-setctime is a small Python utility designed to set the creation time (ctime) of files and directories on Windows operating systems. It currently is at version 1.2.0 and has an infrequent, but active, release cadence, with updates typically addressing bug fixes, accuracy improvements, or new features.
Warnings
- gotcha This library is strictly for Windows operating systems. Attempting to use `win32-setctime` on non-Windows platforms will result in errors, as it relies on Windows-specific kernel32 DLL calls.
- gotcha Before version 1.0.4, `setctime` could have accuracy issues when converting very large or very small timestamps, leading to slight inaccuracies in the set creation time.
- gotcha The `setctime` function, by default, follows symbolic links. If you need to set the creation time of the symbolic link itself rather than its target, you must explicitly pass `follow_symlinks=False`.
- gotcha Prior to version 1.0.3, `setctime` did not support changing the creation time of directories. Attempts to use it on a directory would likely fail.
Install
-
pip install win32-setctime
Imports
- setctime
from win32_setctime import setctime
Quickstart
import os
import time
from datetime import datetime
from win32_setctime import setctime
# Create a dummy file
file_path = 'my_test_file.txt'
with open(file_path, 'w') as f:
f.write('Hello, world!')
# Get current timestamp
current_timestamp = time.time()
print(f"Current file creation time: {datetime.fromtimestamp(os.path.getctime(file_path))}")
# Set a new creation time (e.g., 1 day in the past)
new_timestamp = current_timestamp - (24 * 60 * 60)
setctime(file_path, new_timestamp)
print(f"New file creation time: {datetime.fromtimestamp(os.path.getctime(file_path))}")
# Clean up
os.remove(file_path)