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
|
# ABSTRACT: Batch-rename all files in a directory
# This code changes the file suffix of all files in a directory
# ending in *.log so that they end in *.txt
#
# Note - This example is NOT recursive.
use strict;
use warnings;
use vars qw( $dir );
# Regarding "SL" below: On Win/DOS, it is "\" and on Mac/BSD/Linux it is "/"
# File::Util will automatically detect this for you.
use File::Util qw( NL SL );
my $ftl = File::Util->new();
my $dir = 'some/log/directory';
my @files = $ftl->list_dir( $dir, '--files-only' );
foreach my $file ( @files ) {
# don't change the file suffix unless it is *.log
next unless $file =~ /log$/;
my $newname = $file;
$newname =~ s/\.log$/\.txt/;
if ( rename $dir . SL . $file, $dir . SL . $newname ) {
print qq($file -> $newname), NL
}
else {
warn qq(Couldn't rename "$_" to "$newname" - $!)
}
}
exit;
|