Advertisement
Guest User

exscape

a guest
Feb 1st, 2009
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.55 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. # massrename.pl - renames files using regular expressions
  4. # Copyright (C) 2008, Thomas Backman <serenity@exscape.org>
  5. # Written on 2008-03-12; last modified 2008-03-16
  6.  
  7. # This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. # You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  10.  
  11. use strict;
  12. use warnings;
  13. use Getopt::Std;
  14. use constant DEBUG => 0;
  15.  
  16. our $VERSION = "0.2";
  17. $Getopt::Std::STANDARD_HELP_VERSION = 1;
  18.  
  19. # Get command line arguments
  20. my %options;
  21. getopts("dfpvVh", \%options);
  22.  
  23. sub HELP_MESSAGE
  24. {
  25.     print <<EOF
  26. massrename v$VERSION, by Thomas Backman <serenity\@exscape.org>
  27. Usage: $0 [options...] <file list> <regex>
  28. Valid options:
  29.     -f  Overwrite existing file, if any
  30.     -d  Work on directories as well. By default, only regular files (and symlinks) are renamed.
  31.     -p  Pretend. That is, don't move anything, only display what would be moved without -p active.
  32.     -v  Be verbose (print all old and new filenames)
  33.     -V, -h  Show this help/version screen.
  34.  
  35. Example: $0 *.bak 's/\\.bak\$/.txt/g'
  36. EOF
  37. ;
  38.     exit 0;
  39. }
  40.  
  41. sub VERSION_MESSAGE { &HELP_MESSAGE }
  42.  
  43. HELP_MESSAGE() unless @ARGV >= 2;
  44. HELP_MESSAGE() if ($options{h} or $options{V});
  45.  
  46. # The regex should be the last argument, so:
  47. my $regex = pop @ARGV;
  48.  
  49. # Split the regex. Lets hope it doesn't contain slashes (which isn't *too* common in filenames!)
  50. my ($search, $repl, $flags) = ($1, $2, $3) if $regex =~ m{^s/(.*?)/(.*?)/(\w+)?};
  51. $repl = "" unless defined $repl;
  52. $flags = "" unless defined $flags;
  53. die "Unable to read regex! Make sure it's in the form s/old/new/flags\nFlags are optional, but you might want the /g flag. See man perlre for more info.\n" unless (defined $search && defined $repl);
  54.  
  55. # Walk through the file list and delete all non-files
  56. for my $index (0 .. $#ARGV) {
  57.     $_ = $ARGV[$index];
  58.     unless (-f || -l || -d) {
  59.         warn "$_ doesn't appear to be an existing file, ignoring\n";
  60.         if (DEBUG) { print ">>> Deleting $ARGV[$index] from the file list...\n" }
  61.         delete $ARGV[$index];
  62.     }
  63. }
  64.  
  65. # Main loop follows, I guess.
  66. for my $filename (@ARGV) {
  67.     my $newname = $filename;
  68.  
  69.     # Couldn't get it to work any other way.
  70.     eval '$newname =~ s/$search/' . "$repl/$flags";
  71.     die "Unable to execute substitution! Invalid regular expression?\nError message was: $@\n" if $@;
  72.     do_move ($filename, $newname) unless $filename eq $newname; # Ignore files whose name didn't change
  73. }
  74.  
  75. sub do_move {
  76.     my ($filename, $newname) = @_;
  77.  
  78.     if (-e $newname && !$options{f}) {
  79.         warn "$newname already exists, not renaming $filename (use -f switch to force)\n";
  80.         return
  81.     }
  82.     if (-d $filename && !$options{d}) {
  83.         warn "$filename is a directory, skipping (use -d switch to rename directories as well)\n";
  84.         return
  85.     }
  86.  
  87.     # Print names if debugging, pretending (-p) or told to be verbose (-v)
  88.     if (DEBUG || $options{p} || $options{v}) { print ">>> $filename -> $newname\n"; }
  89.     return if $options{p}; # Don't actually rename if we're pretending
  90.     rename $filename, $newname or warn "Unable to rename $filename to $newname: $!\n";
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement