这里是perl中的一个示例。
您可能想修改这个,但是它展示了如何使用opendir/readdir,这是我认为需要的。它可能会变得更短,但可能不像python版本那么好看,所以我详细地说了一遍(希望)清楚:)#!/usr/bin/env perl
#
use warnings;
use strict;
my $rootdir = 'your/root/dir';
opendir( my $rootdh, $rootdir ) || die;
foreach my $dir ( readdir $rootdh ) {
# skip over the special directories . and ..
if ($dir =~ m/^\./) {
next;
}
# only want directories
next unless ( -d "$rootdir/$dir" );
opendir( my $dh, "$rootdir/$dir" ) || die;
foreach my $file ( readdir $dh ) {
if ($file =~ m/^\./) {
next;
}
# only want files this time
next unless ( -f "$rootdir/$dir/$file" );
my $extension = $file;
$extension =~ s/.*\.//g;
print "$rootdir/$dir/$file", " will be renamed to: ", "$rootdir/$dir/$dir.$extension", "\n";
# uncomment this when you're ready!
#rename "$rootdir/$dir/$file" "$rootdir/$dir/$dir.$extension";
}
}