Archive for 'perl'

renner - hacky little file renaming utility

Monday, 12 November 2007

Even in these days of graphical development environments, I often find myself needing to change the extensions of hundreds of files, or remove “_converted” from the end of a file name, or zero-pad the beginning of a file name so “001″ “2″ and “03″ will sort correctly.

Renner is a little perl script I hacked together to do all those tasks. It’s not pretty, but it’s gotten the job done for me for years. Mostly I’m putting it on the Internets so it’s easy for me to find it when I need it.

How to use it:

Summary Help

perl renner.pl location [matchcriterion] action [utility]
S = a string
n = a number (of characters)
match criteria:
-mask S|-unmask S
-dir
actions:prepend/append/unprepend/unappend(truncate):
-app S| -pre S| -unapp n| -unpre n
actions:filename substitutions:
-mask S1 -sub S2
-dig n1 n2
recursion:
-r
utility:
-help | -silent | -test | -v

Example

Add .jpg to all non jpg files
perl /pathto/renner.pl . -unmask jpg$ -app .jpg

Match Criteria

$ - anchors a match to END of string
^ - anchors a match to START of string

-mask string
default: .
act only on filenames that match the string
example: perl renner.pl . -mask .txt -app .bak
append .bak to any file in currect directory matching .txt
foo.txt -> foo.txt.bak
foo.txt.bak -> foo.txt.bak.txt
example: renner.pl . -mask .txt$ -app .bak
append .bak to any file with extension .txt
foo.txt -> foo.txt.bak
foo.txt.bak -> foo.txt.bak
-unmask string
act only on filenames that do NOT match the string
example: renner.pl . -unmask ^_ -app .txt
append .txt to any file in current directory that does NOT
begin with underscore
_foo -> foo
foo -> foo.txt
NOTE: -mask and -umask may not be used in the same operation.

-dir
Rename directories in addition to plain files.
NOTE: -dir and -r (recursion) may not be used in the same operation.

Add or Remove Strings from Filename

-app string
-pre string
append or prepend string to all matching filenames
example: renner.pl . -app .bak
append .bak to all files in current directory
foo -> foo.bak
example: renner.pl . -mask .gif$ -pre _
prepend all .gif files in current directory with underscore
foo.gif -> _foo.gif
foo.txt -> foo.txt

-unapp integer
-unpre integer
delete integer characters from start or end of filename.
example: renner.pl . -mask .txt$ -unapp 4
delete extension from all txt files in current directory
foo.txt -> foo
example: renner.pl . -unmask . -unapp 1
delete leading character from all files in current directory with
no extension
afoo -> foo

Perform Substitutions on Filename

-mask searchstring - sub replacestring
replace searchstring in filename with replacestring. searchstring
is required in this form.
example: renner.pl -mask display -sub d
display1.jpg -> d1.jpg

-dig integer1 integer2
zeropad strings of integer1 consecutive digits to integer2 digits
example: renner.pl -mask .txt$ -dig 2 3
23.txt -> 023.txt
123.txt -> 123.txt
To include a space in a mask, sub, or unmask, use $space

Recursion

-r
recursively traverse directories.
NOTE: -r and -dir (rename directories) may not be used in the same operation.

Utility

-help
print this message

-silent
suppress messages

-test
describe action but don’t actually rename files

-v
print detailed messages