Skip to content

Instantly share code, notes, and snippets.

@enghqii
Created June 8, 2020 07:27
Show Gist options
  • Save enghqii/44dc0941db612120e4f94fe95d460180 to your computer and use it in GitHub Desktop.
Save enghqii/44dc0941db612120e4f94fe95d460180 to your computer and use it in GitHub Desktop.
A python script that overwrites file creation time
#! /usr/bin/python
import os, sys
import platform
import time
# pip install pypiwin32
import pywintypes, win32file, win32con
def changeFileCreationTime(fname, newtime):
wintime = pywintypes.Time(newtime)
winfile = win32file.CreateFile(
fname, win32con.GENERIC_WRITE,
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE,
None, win32con.OPEN_EXISTING,
win32con.FILE_ATTRIBUTE_NORMAL, None)
win32file.SetFileTime(winfile, wintime, None, None)
winfile.close()
def oldfy(path):
print(path)
for root, dirs, files in os.walk(path, topdown=False):
# per file
for file in files:
filepath = os.path.join(root, file)
stat = os.stat(filepath)
if stat.st_mtime < stat.st_ctime:
print(time.ctime(stat.st_ctime) + " | " + time.ctime(stat.st_mtime))
changeFileCreationTime(filepath, stat.st_mtime)
for dir in dirs:
dirpath = os.path.join(root, dir)
oldfy(dirpath)
path = sys.argv[1]
print("Oldfy")
print(platform.system())
print("path: " + path)
print("")
oldfy(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment