The following Perl Script performs a mass change file extension to files under a specific directory. In the given example, it will rename all .dat files to .prn files.
1st Step
Create a Perl Script named massrename.pl - here's the code:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #!/bin/perl # rename: renames files according to the expr given on the command line. # The expr will usually be a 's' or 'y' command, but can be any valid # perl command if it makes sense. Takes a list of files to work on or # defaults to '*' in the current directory. # e.g. # rename 's/\.flip$/.flop/' # rename *.flip to *.flop # rename s/flip/flop/ # rename *flip* to *flop* # rename 's/^s\.(.*)/$1.X/' # switch sccs filenames around # rename 's/$/.orig/' */*.[ch] # add .orig to your source files in */ # rename 'y/A-Z/a-z/' # lowercase all filenames in . # rename 'y/A-Z/a-z/ if -B' # same, but just binaries! # rename chop *~ # restore all ~ backup files use Getopt::Std; my ($subst, $name); if (!&getopts("nfq") || $#ARGV == -1) { die "Usage: rename [-fnq] <perl expression> [file file...] -f : Force the new filename even if it exists already -n : Just print what would happen, but don't do the command -q : Don't print the files as they are renamed e.g. : rename 's/\.c/.c.old/' * rename -q 'y/A-Z/a-z/' *\n"; } $subst = shift; # Get perl command to work on @ARGV = <*> if $#ARGV < 0; # Default to complete directory foreach $name (@ARGV) { $_ = $name; eval "$subst;"; die $@ if $@; next if -e $_ && !$opt_f; # Skip if the file exists if asked to. mext if $_ eq $name; if ($opt_n) { print "mv $name $_\n"; next; } print "mv $name $_\n" if !$opt_q; rename($name,$_) or warn "Can't rename $name to $_, $!\n"; } |
2nd Step
Execute the following command to mass change extension to your files. In the following example, we will rename all .dat files to .prn :massrename 's/\.prn.*/\.dat/'
No comments:
Post a Comment