bulk rename pod1 > pod2 needed

Kevin Spencer kevin at kevinspencer.org
Tue Feb 17 13:37:59 MST 2009


On Mon, Feb 16, 2009 at 5:33 PM, Dennis Kibbe <dennisk at sdf.lonestar.org> wrote:
> Can someone suggest a way to rename a directory of files like this:
>
> ccna.pod1.pc1.ext is renamed to ccna.pod2.pc1.ext
> ccna.pod2.pc1.ext is renamed to ccna.pod3.pc1.ext
>
> and so on.
>
> (Aside: Hans, if I were able to take your scripting class I won't be asking this now. :-)
>
> Dennisk

A quick Perl solution.

#!/usr/bin/perl -w

use strict;

my $indir = '/path-to/the-files';

opendir(DIRECTORY, $indir) || die "Could not open $indir - $!\n";
my @files = grep /\.ext$/, readdir(DIRECTORY);
closedir(DIRECTORY);
@files = reverse(sort(@files));
foreach my $oldfilename (@files) {
    if ($oldfilename =~ /^ccna.pod(\d+)/) {
        my $sequence = $1 + 1;
        my $newfilename = 'ccna.pod' . $sequence .'.pc1.ext';
        rename($oldfilename, $newfilename) || die "Could not rename
$oldfilename to $newfilename - $!\n";
    }
}

Kevin.


More information about the PLUG-discuss mailing list