Wrote Python code to repair file dates
From
Stefan Ram@21:1/5 to
All on Tue Dec 24 12:48:59 2024
On my disks, there are some files with a future modification
time, like for example, in the year of 2100.
The following Python script tries to detect and modify such
dates. If one of the dates of creation or modification is
in the future, it will be set to the other. When both are
in the future, they will be set to the current date and time.
Now, I personally am using this, but beware, it's not extensively
tested, so the risks of executing it are not calculatable, so
I'm publishing it just for reading, not for executing it without
proper code review and further extensive code testing!
import os
import datetime
import ctypes
from ctypes import windll, wintypes, byref
def set_file_times(file_path, creation_time, modification_time):
# Set modification time
os.utime(file_path, (modification_time.timestamp(), modification_time.timestamp()))
# Set creation time (Windows only)
wintime = int((creation_time.timestamp() * 10000000) + 116444736000000000)
ctime = wintypes.FILETIME(wintime & 0xFFFFFFFF, wintime >> 32)
handle = windll.kernel32.CreateFileW(file_path, 256, 0, None, 3, 128, None)
windll.kernel32.SetFileTime(handle, byref(ctime), None, None)
windll.kernel32.CloseHandle(handle)
def process_files( directory ):
current_time = datetime.datetime.now()
for root, _, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
creation_time = datetime.datetime.fromtimestamp(os.path.getctime(file_path))
modification_time = datetime.datetime.fromtimestamp(os.path.getmtime(file_path))
if creation_time > current_time and modification_time <= current_time:
set_file_times(file_path, modification_time, modification_time)
print(f"Updated creation time for {file_path}")
elif modification_time > current_time and creation_time <= current_time:
set_file_times(file_path, creation_time, creation_time)
print(f"Updated modification time for {file_path}")
elif creation_time > current_time and modification_time > current_time:
set_file_times(file_path, current_time, current_time)
print(f"Updated both creation and modification times for {file_path}")
if __name__ == "__main__":
# process the files in this directory and in all its subdirectories
directory = "C:\\example"
process_files( directory )
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)