slitaz-dev-tools view baba-scripts/reninc.pl @ rev 238

tazwikiss: add min, max, cnt
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Feb 22 11:10:22 2015 +0100 (2015-02-22)
parents
children
line source
1 #! /usr/bin/perl
2 #check for help
3 chomp (my $workdir = `pwd`);
4 chomp (my $scriptname=`basename "$0"`);
5 scalar @ARGV > 0 || die <<EOT1;
6 Usage : $scriptname [-psdn [argument]] directory|here
8 Options :
9 -p prefix
10 -s suffix
11 -d starting_base_number
12 -n length of number part (1: one number, 2: two numbers, etc)
14 Parameters :
15 directory : complete path of directory to process, or "here" for
16 working directory
17 ($workdir)
18 EOT1
19 #check arguments
20 my ($pref, $suff, $start, $num_length) = ('', '', 1, 1);
21 while (scalar @ARGV > 0)
22 {
23 my $arg = shift @ARGV;
24 if ($arg eq "-p") { $pref = shift @ARGV; next; }
25 elsif ($arg eq "-s") { $suff = shift @ARGV; next; }
26 elsif ($arg eq "-d") { $start = shift @ARGV; next; }
27 elsif ($arg eq "-n") { $num_length = shift(@ARGV) - 1; next; }
28 elsif (-d $arg) { $dir = shift @ARGV; next; }
29 elsif ($arg eq "here") { $dir = $workdir; next; }
30 }
31 #main routine
32 chdir $dir;
33 foreach (<*>)
34 {
35 my $counter = $start;
36 my ($purename, $ext) = (m/^(.+?)\.([^\.]+)$/);
37 for (my $n = 1; $n <= $num_length; $n++)
38 {
39 last if ($num_length eq 0);
40 if ($start < 10**$n) { $counter = '0'.$counter; }
41 }
42 $start++;
43 print "Rename \"$_\" in $pref$counter$suff.$ext\n";
44 rename $_, $pref.$counter.$suff.'.'.$ext;
45 }