Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Name Mangler - A batch file renaming utility' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Name Mangler - A batch file renaming utility
Authored by: phoobahr on Jun 09, '08 03:04:23PM
I've found myself doing most of my file renaming from the console. To make things easier I wrote this:

 cat $(which mmv)
#! /usr/bin/python

"""
mmv: Multiple move.
A small utility designed to rename files in the current working directory both matching against, and transforming the matches via python regular expressions.
"""

import re,os,os.path,sys
from optparse import OptionParser


parser = OptionParser(usage="Usage: %prog [OPTIONS] $regex_match $regex_sub\nExample: mmv -t \".*([0-9]*).mp3\" \"Track \\1.mp3\"",version="%prog 0.8.1")
parser.add_option("-t","--test",action="store_true",default=False,dest="test",help="Test. Display all output but do not commit new filenames.")
(options,args) = parser.parse_args()

if 2 != len(args):
	parser.error("Incorrect number of arguments.")
try:
	regex = re.compile(args[0])
except Exception,e:
	parser.error("Error parsing $regex_match.")
else:
	if options.test:
		mmv = lambda src,dst: "testing: %s:\t%s" % (src,dst)
	else:
		mmv = lambda src,dst: os.rename(src,dst)
	for ix in os.listdir('./'):
		if re.search(args[0],ix):
			try:
				dst = regex.sub(args[1],ix)
				result = mmv(ix,dst)
				if result == None:
					result = "%s:\t%s" % (ix,dst)
			except Exception,e:
				result = e.__doc__
			print result


[ Reply to This | # ]