Advertisement
furicle

Perl based rename

Jan 16th, 2020
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.17 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. # $Revision: 331 $$Date: 2013-04-30 21:23:41 +0100 (Tue, 30 Apr 2013) $
  3. # Robin's RCS header:
  4. # RCSfile: rename.PL,v Revision: 1.3   Date: 2006/05/25 09:20:32
  5. # Larry's RCS header:
  6. #  RCSfile: rename,v   Revision: 4.1   Date: 92/08/07 17:20:30
  7. #
  8. #  Log: rename,v
  9. # Revision 1.5  1998/12/18 16:16:31  rmb1
  10. # moved to perl/source
  11. # changed man documentation to POD
  12. #
  13. # Revision 1.4  1997/02/27  17:19:26  rmb1
  14. # corrected usage string
  15. #
  16. # Revision 1.3  1997/02/27  16:39:07  rmb1
  17. # added -v
  18. #
  19. # Revision 1.2  1997/02/27  16:15:40  rmb1
  20. # *** empty log message ***
  21. #
  22. # Revision 1.1  1997/02/27  15:48:51  rmb1
  23. # Initial revision
  24. #
  25.  
  26. use strict;
  27. use File::Rename ();
  28. use Pod::Usage;
  29.  
  30. main() unless caller;
  31.  
  32. sub main {
  33.     my $options = File::Rename::Options::GetOptions
  34.         or pod2usage;
  35.  
  36.     mod_version() if $options->{show_version};
  37.     pod2usage( -verbose => 2 ) if $options->{show_manual};
  38.     pod2usage( -exitval => 1 ) if $options->{show_help};
  39.  
  40.     @ARGV = map {glob} @ARGV if $^O =~ m{Win}msx;
  41.  
  42.     File::Rename::rename(\@ARGV, $options);
  43. }
  44.  
  45. sub mod_version {
  46.     print __FILE__ .
  47.     ' using File::Rename version '.
  48.         $File::Rename::VERSION ."\n\n";
  49.     exit 0
  50. }  
  51.  
  52. 1;
  53.  
  54. __END__
  55.  
  56. =head1 NAME
  57.  
  58. rename - renames multiple files
  59.  
  60. =head1 SYNOPSIS
  61.  
  62. B<rename>
  63. S<[ B<-h>|B<-m>|B<-V> ]>
  64. S<[ B<-v> ]>
  65. S<[ B<-n> ]>
  66. S<[ B<-f> ]>
  67. S<[ B<-e>|B<-E> I<perlexpr>]*|I<perlexpr>>
  68. S<[ I<files> ]>
  69.  
  70. =head1 DESCRIPTION
  71.  
  72. C<rename>
  73. renames the filenames supplied according to the rule specified as the
  74. first argument.
  75. The I<perlexpr>
  76. argument is a Perl expression which is expected to modify the C<$_>
  77. string in Perl for at least some of the filenames specified.
  78. If a given filename is not modified by the expression, it will not be
  79. renamed.
  80. If no filenames are given on the command line, filenames will be read
  81. via standard input.
  82.  
  83. For example, to rename all files matching C<*.bak> to strip the extension,
  84. you might say
  85.  
  86.     rename 's/\e.bak$//' *.bak
  87.  
  88. To translate uppercase names to lower, you'd use
  89.  
  90.     rename 'y/A-Z/a-z/' *
  91.  
  92. =head1 OPTIONS
  93.  
  94. =over 8
  95.  
  96. =item B<-v>, B<-verbose>
  97.  
  98. Verbose: print names of files successfully renamed.
  99.  
  100. =item B<-n>, B<-nono>
  101.  
  102. No action: print names of files to be renamed, but don't rename.
  103.  
  104. =item B<-f>, B<-force>
  105.  
  106. Over write: allow existing files to be over-written.
  107.  
  108. =item B<-h>, B<-help>
  109.  
  110. Help: print SYNOPSIS and OPTIONS.
  111.  
  112. =item B<-m>, B<-man>
  113.  
  114. Manual: print manual page.
  115.  
  116. =item B<-V>, B<-version>
  117.  
  118. Version: show version number.
  119.  
  120. =item B<-e>
  121.  
  122. Expression: code to act on files name.
  123.  
  124. May be repeated to build up code (like C<perl -e>).
  125. If no B<-e>, the first argument is used as code.
  126.  
  127. =item B<-E>
  128.  
  129. Statement: code to act on files name, as B<-e> but terminated by ';'.
  130.  
  131. =back
  132.  
  133. =head1 ENVIRONMENT
  134.  
  135. No environment variables are used.
  136.  
  137. =head1 AUTHOR
  138.  
  139. Larry Wall
  140.  
  141. =head1 SEE ALSO
  142.  
  143. mv(1), perl(1)
  144.  
  145. =head1 DIAGNOSTICS
  146.  
  147. If you give an invalid Perl expression you'll get a syntax error.
  148.  
  149. =head1 BUGS
  150.  
  151. The original
  152. C<rename>
  153. did not check for the existence of target filenames,
  154. so had to be used with care.
  155. I hope I've fixed that (Robin Barker).
  156.  
  157. =cut
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement