#!/usr/bin/env python
#coding=utf-8
#
#
from Cocoa import *
import re

path = "/Users/ssp/Documents/iChats/"
fm = NSFileManager.defaultManager()
filenames, error = fm.contentsOfDirectoryAtPath_error_(path, None)

pattern = "([0-9][0-9])\.([0-9][0-9])\.(200[0-9])"
r = re.compile(pattern)
replacement = r'\3-\2-\1'


print "Using folder " + path
for fileName in filenames:
	if r.match(fileName) != None:
		newName = r.sub(replacement, fileName)
		oldPath = path + fileName
		newPath = path + newName
		success, error =  fm.moveItemAtPath_toPath_error_(oldPath, newPath, None)
		if success:
			print "Renamed " + fileName + " to " + newName + ""
		else:
			print "ERROR trying to rename " + fileName + " to " + newName + ""
