#!/usr/local/bin/perl eval '(exit $?0)' && eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}' & eval 'exec /usr/local/bin/perl -S $0 $argv:q' if 0; # # This program will rename all files found matching pattern to # the pattern that is input for the output file names. # print "Enter file names to be changed using * : "; $patin = <STDIN>; chop($patin); $patin =~ s/\./\\./g; $patin =~ s/\*/(\.*)/g; opendir(PDIR,"."); @files = sort(grep(/^$patin$/,readdir(PDIR))); closedir(PDIR); while ($#files < 0) { print "No files found that match what was entered.\n"; print "Please try again.\n"; print "Enter file names to be changed using * : "; $patin = <STDIN>; chop($patin); $patin =~ s/\./\\./g; $patin =~ s/\*/(\.*)/g; opendir(PDIR,"."); @files = sort(grep(/^$patin$/,readdir(PDIR))); closedir(PDIR); } print "Enter new file names: "; $patout = <STDIN>; chop($patout); print "The following file names will be changed:\n"; foreach $file (@files) { $newfile = $file; $newfile =~ /^$patin$/; $part[1] = $1; $part[2] = $2; $part[3] = $3; $part[4] = $4; $part[5] = $5; $part[6] = $6; $paren = 1; $pos = 0; $newfile = ""; while (($out = index($patout,"*",$pos)) >= $pos) { if ($paren == 1) { if ($out != 0) { $newfile = substr($patout,0,$out-1); } } else { if ($pos+1 < length($patout)) { $newfile .= substr($patout,$pos,$out-$pos); } } $newfile .= $part[$paren]; $paren++; $pos = $out + 1; } if ($pos < length($patout)) { $newfile .= substr($patout,$pos); } print "$file changed to $newfile\n"; } print "Is this correct? (y/n) "; $ans = <STDIN>; chop($ans); while (! ($ans =~ /[nNyY]/)) { print "Invalid answer entered. Please try again.\n"; print "Is this correct? (y/n) "; $ans = <STDIN>; chop($ans); } if ($ans =~ /[yY]/) { foreach $file (@files) { $newfile = $file; $newfile =~ /^$patin$/; $part[1] = $1; $part[2] = $2; $part[3] = $3; $part[4] = $4; $part[5] = $5; $part[6] = $6; $paren = 1; $pos = 0; $newfile = ""; while (($out = index($patout,"*",$pos)) >= $pos) { if ($paren == 1) { if ($out != 0) { $newfile = substr($patout,0,$out-1); } } else { if ($pos+1 < length($patout)) { $newfile .= substr($patout,$pos,$out-$pos); } } $newfile .= $part[$paren]; $paren++; $pos = $out + 1; } if ($pos < length($patout)) { $newfile .= substr($patout,$pos); } if (! rename($file,$newfile)) { print "$file could not be renamed.\n"; } } }