Tuesday 13 April 2010

Batch renamer using python string methods

I'll admit it: I'm crap at regex. I find python's string manipulation a lot more intuitive, and I'm finding this script a lot quicker to work with than any GUI renaming tools or the bash shell, too.

Add it to your nautilus or thunar action menu and work with the filenames like you've put them all in a python list and are writing the for loop expression


You have a few variables available to you: the filename, it's basename, it's extention and the element's index within the list. It lets you do most things to strings that python lets you do since it passes your command directly to python. The only limitation is that it need to be a one-liner, and it needs to return a string. Click here to familiarise yourself with python's string methods.

It'll let you confirm before applying changes, and reset if you tell it not to confirm.



Here are some examples from the help section if you're new to python:

n = b.replace('foo', 'bar')+e
Renames feefoobar.txt to feebarbar.txt
Replaces foo with bar in basename, and appends the extention

n = n.split('foo')[0]+'.log'
Renames feefoobar.txt to fee.log
Splits the filename at 'foo' and returns the part before the split, and appends a .log extention

n = b+str(i)+e
Renames feefoobar.txt to feefoobar0.txt
Adds the current element's index integer as a string to the end of the basename, then appends the extention


Click here to get it

Edit: This has been updated 

No comments:

Post a Comment