"""
@author: [email protected]
@file: change_files_times.py
@time: 2023/11/22 11:55
@desc:
"""
import os
import time
import ctypes
def parse_date(date):
"""
2023-12-12 12:12:12 解析时间
"""
head,tail = date.split(" ")
year,month,day = head.split("-")
hour,min,sec = tail.split(":")
return year,month,day,hour,min,sec
def change_file_creation_time(file_path, year,month,day,hour,min,sec):
"""
修改文件更改时间
"""
new_creation_time = time.mktime((int(year), int(month), int(day), int(hour), int(min), int(sec), 0, 0, 0))
access_time = os.path.getatime(file_path)
modified_time = os.path.getmtime(file_path)
os.utime(file_path, (access_time, new_creation_time))
def change_system_times(year,month,day,hour,min,sec,msec="0"):
"""
调用系统接口更改系统时间,隔一段时间系统时钟会自动更新回当前真实时间
"""
class SYSTEMTIME(ctypes.Structure):
_fields_ = [
('wYear', ctypes.c_ushort),
('wMonth', ctypes.c_ushort),
('wDayOfWeek', ctypes.c_ushort),
('wDay', ctypes.c_ushort),
('wHour', ctypes.c_ushort),
('wMinute', ctypes.c_ushort),
('wSecond', ctypes.c_ushort),
('wMilliseconds', ctypes.c_ushort)
]
target_date = SYSTEMTIME(
wYear=int(year),
wMonth=int(month),
wDayOfWeek=0,
wDay=int(day),
wHour=int(hour)-8,
wMinute=int(min),
wSecond=int(sec),
wMilliseconds=int(msec)
)
ctypes.windll.kernel32.SetSystemTime(ctypes.byref(target_date))
def cp_files(file,out_path):
"""
复制文件,实现更改文件创建时间
"""
name = os.path.split(file)[-1]
save_file = os.path.join(out_path,name)
with open(file,"rb")as fp,\
open(save_file,"wb")as fp2:
fp2.write(fp.read())
return save_file
def main(create_time,change_time,in_file,out_path):
"""
处理流程
"""
year, month, day, hour, min, sec = parse_date(create_time)
change_system_times(year, month, day, hour, min, sec)
save_file = cp_files(in_file,out_path)
year, month, day, hour, min, sec = parse_date(change_time)
change_file_creation_time(save_file, year,month,day,hour,min,sec)
if __name__ == '__main__':
in_file = input("请输入要修改文件路径:")
out_path = input("请输入更改时间后文件保存路径:")
create_time = input("请输入文件创建时间:[2023-12-12 12:12:12]")
change_time = input("请输入文件修改时间:[2023-12-12 12:12:12]")
main(create_time,change_time,in_file,out_path)