This is the old SliTaz forum - Please use the main forum.slitaz.org

[Perl] reninc.pl : incremental renaming
  • babaorumbabaorum March 2010
    Hi, I've written a small Perl script to incrementally rename all filenames within a directory.
    This feature is simple but useful. The script processes a whole directory. You can specify what number lenth you desire (the missing numbers will be filled with leading zeros if necessary), a prefix, a suffix and a starting number; and of course, the directory (default is current working directory).
    Don't forget to enable execute mode after putting the script in a $PATH directory:
    su
    chmod +x reninc.pl

    Written for Perl 5.10.0, on a SliTaz stable 2.0.

    #!/usr/bin/perl
    #reninc.pl - incremental renaming
    #check for help
    chomp (my $workdir = `pwd`);
    chomp (my $scriptname=`basename "$0"`);
    scalar @ARGV > 0 || die <<EOT1;
    Usage : $scriptname [-options] directory

    Options :
    -p prefix
    -s suffix
    -d starting_base_number
    -n length of number part (1: one number, 2: two numbers, etc)

    Parameters :
    directory : complete path of directory to process, or "here" for
    working directory
    ($workdir)
    EOT1
    #check arguments
    my ($pref, $suff, $start, $num_length) = ('', '', 1, 1);
    while (scalar @ARGV > 0)
    {
    my $arg = shift @ARGV;
    if ($arg eq "-p") { $pref = shift @ARGV; next; }
    elsif ($arg eq "-s") { $suff = shift @ARGV; next; }
    elsif ($arg eq "-d") { $start = shift @ARGV; next; }
    elsif ($arg eq "-n") { $num_length = shift @ARGV; next; }
    elsif (-d $arg) { $dir = shift @ARGV; next; }
    elsif ($arg eq "here") { $dir = $workdir; next; }
    }
    #main routine
    chdir $dir;
    foreach (<*>)
    {
    my $counter = $start;
    my ($purename, $ext) = (m/^(.+?)\.([^\.]+)$/);
    for (my $n = 1; $n <= $num_length; $n++)<br /> {
    last if ($num_length eq 0);
    if ($start < 10**$n) { $counter = '0'.$counter; }
    }
    $start++;
    print "Rename \"$_\" in $pref$counter$suff.$ext\n";
    rename $_, $pref.$counter.$suff.'.'.$ext;
    }

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Apply for Membership

SliTaz Social