#!/usr/bin/perl # # An attempt to implement good old DOS command in Perl # Author: Alexey Vikhlinin # Warranty: none # # alias rename 'set noglob; /full/path/rename \!*; unset noglob' # rename *.f *.for # $USAGE = "Usage: rename [-exe] [-reverse] [-command] [-otherargs] from to\n"; $exe = 0; $reverse = 0; $command = "mv"; $otherargs = ""; while ( 1 ) { if ( $#ARGV < 1 ) { die $USAGE; } if ( $ARGV[0] eq "-exe" ) { $exe = 1; shift @ARGV; next; } if ( $ARGV[0] eq "-reverse" ) { $reverse = 1; shift @ARGV; next; } if ( $ARGV[0] eq "-command" ) { shift @ARGV; if ( $#ARGV < 0 ) { die $USAGE; } $command = $ARGV[0]; shift @ARGV; next; } if ( $ARGV[0] eq "-otherargs" ) { shift @ARGV; if ( $#ARGV < 0 ) { die $USAGE; } $otherargs = $ARGV[0]; shift @ARGV; next; } last; } if ( $#ARGV != 1 ) { die $USAGE; } ($from,$to) = @ARGV; $_=$from; $infrom = tr/*/*/; $_=$to; $into = tr/*/*/; die "must be equal number of *\'s in the from- and to- patterns\n" unless $infrom == $into; $From = $from; # save the value to do glob later $from =~ s/\./\\\./g; $from =~ s/\*/(.\*)/g; while ( <${From}> ) { $fileto = $to; $file = $_; /$from/; $one = $1; $two = $2; $three = $3; $four = $4; $five = $5; $six = $6; $seven = $7; $fileto =~ s/\*/$one/; $fileto =~ s/\*/$two/; $fileto =~ s/\*/$three/; $fileto =~ s/\*/$four/; $fileto =~ s/\*/$five/; $fileto =~ s/\*/$six/; $fileto =~ s/\*/$seven/; if ( $reverse ) { ($file,$fileto) = ($fileto,$file); } if ( $exe ) { if ( $command eq "mv" ) { rename ($file,$fileto) unless $file eq $fileto; } else { if ( $otherargs ) { system ("$command $file $fileto $otherargs"); } else { system ("$command $file $fileto"); } } } else { if ( $command eq "mv" ) { print "$command ",$file," ",$fileto,"$otherargs\n" unless $file eq $fileto; } else { print "$command ",$file," ",$fileto,"$otherargs\n"; } } }