#!/usr/bin/perl # # The filename regular expression renamer. # # Usage: frep [-p] perlexpr [files] # # Ex: frep s/.wav.mp3/.mp3/o *.mp3 # # or if a list of files is on the standard input # and no arguments past the expression are given # the program will rename each file listed. # # Ex: ls | frep s/.txt/.csv/o # while (@ARGV and $ARGV[0] =~ /^-p$/o) { shift; $pretend = 1; } ($regexp = shift @ARGV) || die "Usage: frep [-p] perlexpr [filenames]\n"; if (!@ARGV) { @ARGV = ; chomp(@ARGV); } foreach (@ARGV) { $old_name = $_; eval $regexp; die $@ if $@; # Kill if regexp didn't execute right. $map{$old_name} = $_ unless $_ eq $old_name; } if ($pretend) { foreach (keys %map) { print "$_ -> $map{$_}\n"; } exit(0); } foreach (keys %map) { if ($check{$map{$_}}) { print "Collision detected. Run with '-p' for details.\n"; exit(0); } $check{$map{$_}} = 1; } foreach (keys %map) { if (-e $map{$_}) { print "Collision detected. Attemted to rename $_ to " . $map{$_} . " but file already exists.\n"; next; } rename($_,$map{$_}); } exit(0);