I am trying to change the directory creation timestamp for a windows system using python. I have a directory that was copied over from another drive and the directory creation times are not preserved. This is what I am hoping to do,
Step 1 : Read the source directory list and thier creation times with the following code,
import os
source_dir_path = r'd:\data'
list_of_directories = os.listdir(source_dir_path)
creation_times = {}
for d in list_of_directories:
creation_times[d] = os.path.getctime(os.path.join(source_dir_path, d))
Step 2 : Iterate over the directory list and set the Directory Creation Time. For this I am relyting on Python For Windows Extensions. Code is shown below,
from win32file import CreateFile, SetFileTime, GetFileTime, CloseHandle
from win32file import GENERIC_READ, GENERIC_WRITE, OPEN_EXISTING
from pywintypes import Time
destination_dir_path = r'f:\data'
#list_of_directories = os.listdir(source_dir_path)
for d in creation_times:
fh = CreateFile(os.path.join(destination_dir_path,d), 0, 0, None, OPEN_EXISTING, 0,0)
SetFileTime(fh, creation_times[d])
I am getting an 'Access is Denied' at the CreateFile line. I am not sure if this a valid way to setting creation time for the directory. Is this the right way set directory creation times