Friday, October 28, 2011

Change file timestamp

A crude python script to change modify/access file timestamps, after the new NAS would incorrectly change them to current time. Note: only changes modify and access time. File create time cannot be changed by standard python calls. From quick online research, it is suggested lower level, OS dependent, privileged system calls or crudely changing the system clock is required to change creation times.
import sys, os, time

sys.exit()  # hardcode the changed timestamp below

stime1 = "08/24/2010  06:24 PM" # everyday food video
stime2 = "06/29/2010  04:31 PM" # neely video
ts = time.strptime(stime2, "%m/%d/%Y  %I:%M %p")

print ts
print time.mktime(ts)
setTimeVal = time.mktime(ts)

print sys.argv[0]
print len(sys.argv)
if len(sys.argv) < 2:
 print "usage ", sys.argv[0], " <filename>"
 sys.exit(-1)

filename = sys.argv[1]

ts = os.stat(filename)
print ts

os.utime(filename, (setTimeVal,setTimeVal))

No comments:

Post a Comment