Daily Archives: March 8, 2008

Restoring lost Modification Date/Time on My Macintosh Using Python

If you are an avid Dr. Chuck Blog reader – you recall on November 9, 2007 when I upgraded to Leopard for the second time and lost my modification dates on my files. I was too lazy to re-do the upgrade so I was left with a bunch of files with November 9, 2007 modification dates making ls -l look yucky.

http://www.dr-chuck.com/csev-blog/000139.html

I made a mistake of not reading my own earlier blog entry telling me how to properly backup and restore my home directory over a Mac OS upgrade.

http://www.dr-chuck.com/csev-blog/000381.html

Apparently I am not such an avid reader of the Dr. Chuck Blog.

Well today I rectified the situation and have my files dating back to 2004 and earlier – I don’t have a bunch of files tantalizingly telling me “November 9, 2007” – teasing me and telling me that the actual date was much earlier. I also figured I should do some Python coding this week to get up on my game as I go to Pycon 08 in Chicago next week – and hang out with none other than Steven Githens- the official badge maker of Pycon 2008!

I had saved that backup from November 2007 and vowed to write some code that would scan my current home directory and look for files with suspect modification dates (right around November 9, 2007) and grab the modification dates form the backup and patch my current home directory.

Then and only then would I be willing to “let that backup file go”. It was keeping me from reformatting a nice 320GB USB drive that I had orther uses for…

So I wrote some Python – below that uses the nice Python os.walk() capability along with the os.stat() capability, and os.utime() capability.

It turned out to be surprisingly elegant and simple in Python.

It ran pretty quickly in about 5 minutes (even reading off USB) and ended up patching about 110,000 files with the right modification date in my laptop home directory. I had a couple copies of my home directory (Mac Air, Mac Pro, and my 320GB extended home directory on USB drive) – and I patched them all.

I include the code below – with the utime() call commented out – so it does a dry run until you are happy with the results and then uncomment the utime(). It nicely can be run over and over – it only patches files in the suspect date range – once the files are moved to their proper earlier time, they are no longer candidates for patching. You can even run it live decide to freak out – press CTRL-C and start over – it will silently glide right past the already patched files.

Continue reading