# rename all files and folders
# renameFiles.py
import os
from time import strftime,localtime
'''
Rename All File and Folders
'''
def renameAll(f,root):
fpath = os.path.join(root,f)
if(f.startswith('212p-999')):
n = f.replace('212p-999','212p-998')
npath = os.path.join(root,n)
os.renames(fpath,npath)
print 'Renamed : '+fpath
l.write('\n'+'Renamed : '+fpath)
else:
print 'Skipped : '+fpath
l.write('\n'+'Skipped : '+fpath)
'''
Search and Replace string inside Maya Files
'''
def findReplaceFile(fpath):
print fpath
l.write('\n'+'----------------------------------------------------------------------------------------------------------')
l.write('\n'+fpath)
print "Start Time : " + strftime("%H:%M:%S",localtime())
l.write('\n'+"Start Time : " + strftime("%H:%M:%S",localtime()))
filein = open(fpath,'r')
inputstr = filein.read()
filein.close()
outputstr=inputstr.replace('212p-999','212p-998')
outputstr=outputstr.replace('p212_999','p212_998')
outputstr=outputstr.replace('212p-201_Suitcase.ma','212p-201_suitcase.ma')
fileout=open(fpath,'w')
fileout.write(outputstr)
fileout.close()
print ("End Time : " + strftime("%H:%M:%S",localtime())+'\n')
l.write('\n'+"End Time : " + strftime("%H:%M:%S",localtime()))
'''
Program Starts Here
'''
basedir=raw_input('Enter Root Path : ')
logdir=raw_input('Enter Log Path : ')
logfile = os.path.join(logdir,'logfile1.txt')
l=open(logfile,'w')
l.write('Rename starts'+'\n'+'------------------------'+'\n'+strftime("%H:%M:%S",localtime())+'\n'+'\n')
for root,dirs,files in os.walk(basedir):
for f in files:
renameAll(f,root)
print '------------------------------------'
l.write('\n'+'---------------------------------------------------------------')
for root,dirs,files in os.walk(basedir,topdown=False):
for d in dirs:
renameAll(d,root)
print '\n'+'All Files & Folders Renamed'
l.write('\n'+'\n'+'Files & Folders Renamed')
l.write('\n'+'---------------------------------------------------------------'+'\n\n\n\n\n\n\n')
l.write('\n'+'Search and Replace inside Maya Files'+'\n-------------------------------------------\n')
filec = 0
for root,dirs,files in os.walk(basedir):
for f in files:
fpath=os.path.join(root, f)
if(f.endswith('.ma')):
findReplaceFile(fpath)
filec+=1
print '\n Total Maya Files Restored : ', filec
l.write('\n'+'------------------'+'\n'+'Rename ends'+'\n'+strftime("%H:%M:%S",localtime())+'\n')
l.close()
No comments:
Post a Comment