''' offsetFileNumber.py Description: This script offsets the trailing group of numbers in a file name. user needs to set "path","offset","padding","fileType", and "fileIncrementType" variable then run script. **SEE LIMITATIONS** EXAMPLE: path = "/home/wbehrnes/test" offset = 5 padding = 4 fileType = txt fileIncrementType = "add" /home/wbehrnes/test/myFile1.15.txt would rename to /home/wbehrnes/test/myFile1.0020.txt Author: Walter Behrnes , walter.behrnes@gmail.com History: Version 1.0 - Inital Release LIMITATIONS: Version 1.0 If the found number has a '-' in front of it this could cause problems. EXAMPLE: myfile-1.txt incremented 2 would produce a file myfile1.txt. The script will see the trailing number as -1 * working version ''' #IMPORT MODULES import os, re, sys, string, shutil from PyQt4 import QtGui from PyQt4 import QtCore class OffsetWindow(QtGui.QWidget): #SET PARSE ITEMS global Items, digits, operations Items = [".tif",".jpg",".exr",".tga",".png",".fur",".bgeo",".obj",".iges",".bin",".sd",".txt",".psd",".gif"] Items.sort() digits = ["1","2","3","4","5","6","7","8"] operations = ["add","subtract"] #------------------------------------------------------------------------------# def __init__(self, parent=None): QtGui.QWidget.__init__(self,parent) self.setWindowTitle('OFFSET TOOL 1.0') #WIDGETS self.directoryLabel = QtGui.QLabel("Base Directory",self) self.offsetLabel = QtGui.QLabel("Offset",self) self.paddingLabel = QtGui.QLabel("Padding",self) self.typeLabel = QtGui.QLabel("File Type",self) self.operationLabel = QtGui.QLabel("Offset Type",self) self.previewLabel = QtGui.QLabel("Preview",self) self.directoryEdit = QtGui.QLineEdit(self) self.offsetEdit = QtGui.QLineEdit(self) self.offsetEdit.setText('1') self.paddingComboBox = QtGui.QComboBox(self) self.paddingComboBox.addItems(digits) self.typeComboBox = QtGui.QComboBox(self) self.typeComboBox.addItems(Items) self.operationComboBox = QtGui.QComboBox(self) self.operationComboBox.addItems(operations) self.directoryButton = QtGui.QPushButton("Browse",self) self.renameButton = QtGui.QPushButton("Offset",self) self.previewButton = QtGui.QPushButton("Preview",self) self.showButton = QtGui.QPushButton("View Folder Content",self) self.previewBlock =QtGui.QTextEdit(self) self.previewBlock.setMinimumSize(365,10) #POSITION self.directoryLabel.move(10,15) self.offsetLabel.move(10,45) self.paddingLabel.move(10,75) self.typeLabel.move(10,105) self.operationLabel.move(10,140) self.previewLabel.move(10,200) self.directoryEdit.move(130,10) self.offsetEdit.move(130,40) self.paddingComboBox.move(130,70) self.typeComboBox.move(130,100) self.operationComboBox.move(130,135) self.previewBlock.move(10,220) self.directoryButton.move(300,7) self.previewButton.move(10,170) self.renameButton.move(120,170) self.showButton.move(230,170) #connect signal QtCore.QObject.connect(self.directoryButton, QtCore.SIGNAL("clicked()"), self.on_browse_clicked) QtCore.QObject.connect(self.previewButton,QtCore.SIGNAL("clicked()"), self.on_preview_clicked) QtCore.QObject.connect(self.renameButton,QtCore.SIGNAL("clicked()"),self.on_offset_clicked) QtCore.QObject.connect(self.showButton,QtCore.SIGNAL("clicked()"),self.on_view_clicked) self.resize(385,430) #------------------------------------------------------------------------------# def on_browse_clicked(self): filename = QtGui.QFileDialog.getExistingDirectory(self, 'Select Directory','/home') self.directoryEdit.setText(filename) #------------------------------------------------------------------------------# def on_view_clicked(self): files = self.get_dir_list() files.sort() parseFor = str(self.getParseType()) parsed = self.parse_files(files,parseFor,True) output = "" for fileName in parsed: output += fileName+"\n" self.previewBlock.setText(output) #------------------------------------------------------------------------------# def on_preview_clicked(self): self.getVars(False) #------------------------------------------------------------------------------# def on_offset_clicked(self): self.getVars(True) #------------------------------------------------------------------------------# def get_dir_list(self): outputDir = self.directoryEdit.text() return os.listdir(outputDir) #------------------------------------------------------------------------------# def getParseType(self): whichItem = self.typeComboBox.currentIndex() return self.typeComboBox.itemText(whichItem) #------------------------------------------------------------------------------# def parse_files(self,files,returnType,view): newList = [] for curfile in files: if view: newList.append(curfile) else: if returnType in curfile: newList.append(curfile) if len(newList) == 0: newList.append('NO MATCHING FILES FOUND') return newList #------------------------------------------------------------------------------# def popupWindow(self,title, message): QtGui.QMessageBox.information(self, "%s" % title, "%s" % message, QtGui.QMessageBox.Ok) #------------------------------------------------------------------------------# def getFileList(self,fileType,path): #MAIN CODE fileList = [] #GET LIST OF ITEMS IN DIR dirList=os.listdir(path) #ITER AND MAKE LIST OF FILES TO RENAME count = 0 for fname in dirList: #IF FILE TYPE IN FILENAME if fileType.upper() in fname: count += 1 fileList.append(fname) if fileType.lower() in fname: count += 1 fileList.append(fname) return fileList #------------------------------------------------------------------------------# def getVars(self,operationType): newList = [] #GET PATH path = str(self.directoryEdit.text()) paddingIndex = self.paddingComboBox.currentIndex() padding = self.paddingComboBox.itemText(paddingIndex) offset = self.offsetEdit.text() whichItem = self.typeComboBox.currentIndex() fileType = str(self.typeComboBox.itemText(whichItem)) whichItem = self.operationComboBox.currentIndex() fileIncrementType = str(self.operationComboBox.itemText(whichItem)) #MAIN CODE fileList = self.getFileList(fileType,path) count = len(fileList) if count > 0: if operationType: #CREATE TEMP DIRECTORY self.directoryOperations("create",path) #RENAME FILES PUT IN TEMPDIR SO WE DO NOT HAVE NAMING CONFLICT self.rename(fileList,path,padding,fileIncrementType,offset,operationType) if operationType: #MOVE NEWLY NAMED FILES TO ORIGINAL DIR dirList = os.listdir(path+"/tempRename") for fname in dirList: shutil.move(path+"/tempRename/"+fname,path+"/"+fname) #DELETE TEMP DIR self.directoryOperations("remove",path) self.popupWindow("OFFSET COMPLETE", "FILES OFFSET\nTHANK YOU!") else: output = 'SORRY NO FILES OF TYPE: \"'+fileType+'\" FOUND' self.previewBlock.setText(output) #------------------------------------------------------------------------------# def rename(self,files,path,padding,fileIncrementType,offset,preview): outPutList = [] for fname in files: #find end pattern = '[a-zA-Z./_]*$' p = re.compile(pattern) m = p.findall(fname) end = m[0] #GET NUMBER fnameNoChopEnding = fname[0:len(fname)-len(end)] pattern = '[\-0-9]*$' p = re.compile(pattern) m = p.findall(fnameNoChopEnding) numLen = 0 if m[0]: numLen = len(str(m[0])) number = 0 if fileIncrementType == "add": number = int( m[0] ) + int(offset) else: number = int( m[0] ) - int(offset) #find start start = fname[0:len(fname)-(numLen+len(end))] #CONSTRUCT NAME theNum = str(number) theNum = theNum.zfill(int(padding)) newName = start+theNum+end if preview == False: #RENAME PUT IN TEMP RENAME DIRECTORY outPutList.append(fname+ " RENUMBERED TO: "+newName) else: try: os.rename(path+"/"+fname,path+"/tempRename/"+newName) outPutList.append(fname+ " RENUMBERED TO: "+newName) except: print "Error:\n\t NOT ABLE TO RENAME : \n\t"+path+"/"+fname+" TO "+path+"/tempRename/"+newName+"\n\tPASSING..." else: outPutList.append("SKIPPING: "+fname+" trailing digits not found") print "SKIPPING: "+fname+" trailing digits not found" pass output = "" for items in outPutList: output += items+"\n" self.previewBlock.setText(output) #------------------------------------------------------------------------------# def directoryOperations(self,operation,path): if operation == "create": #CREATE TEMP DIR os.mkdir(path+"/tempRename") if operation == "remove": shutil.rmtree(path+"/tempRename") if __name__ == "__main__": app = QtGui.QApplication(sys.argv) #The Main window qb = OffsetWindow() qb.show() sys.exit(app.exec_())