Read from both STDIN and named files
[project-aon.git] / common / scripts / rename-all.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use File::Copy;
5 use Cwd;
6
7 my $PROGRAM = 'rename-all.pl';
8 my $USAGE = "Usage: $PROGRAM regex substitution [directory]\n\nUses Perl regular expressions to rename multiple files. For example, to change the extension of all files ending with 'TXT' to 'txt':\n\n\t$PROGRAM 'TXT\$' txt\n";
9
10 unless( $#ARGV >= 1 ) { die $USAGE; }
11
12 my $old = shift @ARGV;
13 my $new = shift @ARGV;
14 my $dir = '';
15
16 if( $#ARGV >= 0 ) {
17   $dir = shift @ARGV;
18 }
19 else {
20   $dir = getcwd;
21 }
22
23 opendir( DIR, $dir ) || die( "Cannot open directory: $!\n" );
24 my @files = readdir(DIR);
25 closedir(DIR);
26
27 print $old;
28
29 for my $file (@files) {
30   if( -f $file && $file =~ /$old/ ) {
31     my $oldfile = $file;
32     $file =~ s/$old/$new/;
33     print "mv $oldfile $file\n";
34     move( $oldfile, $file );
35   }
36 }