#!/usr/bin/python # -*- coding: utf-8 -*- #Advanced Renamer python version #Allows mass renaming with regular expressions #(C) Slurdge 2007 - contact : slurdge (a t) slurdge (d o t) org #Licence : GPL : see gpl.txt import sys from sys import exit import os import re program_name = "Advanced Renamer Python Version" version = "1.1" usage = """Usage : """ + sys.argv[0] + r"""[--nocase --noconf|-n -h -d:] -e: -r: --nocase : no case comparison -n or --noconf : no confirmation asked for rename -d : use another directory (use current one if not specified) -h or --help : complete help Type """ + sys.argv[0] + """ -h for complete help.""" regexp = r""" Regulars expressions are in the Python regexp syntax (very similar to Perl). Regular Expressions: Summary Sheet Special Chars: +?.*()[]{}|\ . matches an arbitrary character, but not newline (...) groups a series of patterns elements into a single element + matches a preceding pattern element one or more times ? matches preceding pattern zero or one times * matches preceding pattern zero or more times {N,M} denotes the minimum N and maximum M match count. {N} means exactly N times; {N,} means at least N times [..] denotes a class of characters to match, [^...] negates the class (..|....|..) matches one of the alternatives \ escape character Non alphanumerics: \w matches alphanumeric, including "_" \W matches nonalphanumberic \b matches word boundries \B matches non-boundries \s matches whitespace \S matches non-whitespace \d matches numeric \D matches non-numeric \n,\r,\f,\t have their usual meaning \w,\s,\d may be used within character classes, \b denotes backspace in this context $$1..$$9..$$xxx refer to matched sub-expressions, grouped with (), inside the match To get results, include your expression in (). You can then use $$0 .. $$9...$$xxx in your replace expression. $$0 is the name of the file, $$1 the first expression in (), $$2 the second ... Flags : --nocase : No case comparaison occurs. --noconf or -n : No confirmation is asked. Examples :""" + sys.argv[0] + """ -e:MyFile\.(.+) -r:OtherName.$$1 This will rename every file named MyFile with the original extension.""" if len(sys.argv) < 2: print usage exit() search = None replace = None nocase = None noconf = None directory = u"./" for arg in sys.argv: if arg[:3] == "-e:": try: search = re.compile(arg[3:]) except: print "Error : Problem in compiling your regular expression. Please check your syntax." print usage exit(-3) elif arg[:3] == "-d:": directory = arg[3:] elif arg[:3] == "-r:": replace = arg[3:] elif arg == "--nocase": nocase = True elif arg == "--noconf" or arg == "-n": noconf = True elif (arg == "-h" or arg == "--help"): print usage print regexp exit() if (search is None or replace is None): print "Error : No search or replace expression provided" print usage exit(-2) if not os.path.isdir(directory): print "Error : Invalid directory specified : " + directory print usage exit(-4) flags = 0 if nocase: flags = re.IGNORECASE print program_name + " v" + version print "Working in directory : " + directory files = os.listdir(directory) for file in files: if (os.path.isfile(file)): match = search.match(file,flags) if (match): newfilename = replace newfilename = re.compile("\$\$0").sub(match.group(0),newfilename) group_counter = 1 for group in match.groups(): newfilename = re.compile("\$\$%d" % (group_counter,) ).sub(group,newfilename) group_counter = group_counter + 1 print file.encode("utf-8") + " ==> " + newfilename.encode("utf-8") rename = False if (not noconf): answer = raw_input("Are you sure (y(es)/N(o)/a(ll)/q(No to all)) ?") if (answer == 'A' or answer == 'a'): noconf = True rename = True elif (answer == 'Y' or answer == 'y'): rename = True elif (answer == 'Q' or answer == 'q'): print "Exiting..." exit() else: rename = True if rename: os.rename(file, newfilename)