#!/usr/bin/perl -s ############################################################ # rename # Wed Nov 29 11:06:26 EST 1995 # Meng Weng Wong # # usage: rename function file ... # flags: -i to confirm # -q to suppress "done" message. # -f to force even if target exists # # typically, function will transform an old filename into # a new filename. eg, s/GIF/gif/ # # factoid 19990429 on #nop # if you have a fancy shell, there's probably a way to expand $f from .ext1 to .ext2 in one operation # ie.: for f in *.ext1; do mv $f ${f%.ext1}.ext2; done # in bash ############################################################ if (! @ARGV) { die "usage: rename transform-function file ...\n"; } ($function, @files) = @ARGV; $renamed = 0; $errors = 0; for (@files) { undef($to); eval "(\$to = \$_) =~ $function"; if (defined($to)) { if ($to ne $_) { if (-e $to) { print "rename: $to already exists.\n" unless ($f); next unless ($f || $i); } if ($i) { # interactive print "rename: $_ -> $to [Yn] "; chop($input = <>); next if ($input !~ /^y|^$/i); } rename($_,$to); $renamed++; } } else { print STDERR "error transforming $_\n"; $errors++; } } $numfiles = @files; if (! $q) { print STDERR "$renamed out of $numfiles renamed."; if ($errors) { print STDERR " $errors errors."; } print STDERR "\n"; }