aboutsummaryrefslogtreecommitdiff
path: root/renumfiles
blob: 1f57776e4f2b3d2fd8525e782ef28091defd2493 (plain)
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
39
40
41
42
43
#!/usr/bin/perl -w

sub usage {
	die "Usage: renumfiles <-fake> <startnum> pattern [files ...]\n";
}

if($ARGV[0] =~ /^--?f(?:ake)?$/) {
	$fake = 1;
	shift;
}

$num = 1;
if($ARGV[0] =~ /^\d+$/) {
	$num = $ARGV[0];
	shift;
}

$pattern = $ARGV[0] || usage;
usage if -f $pattern;

shift;
if(@ARGV >= 1000) {
	$places = 4;
} elsif (@ARGV >= 100) {
	$places = 3;
} else {
	$places = 2;
}

$pattern =~ s/\%d/%0${places}d/;
if($pattern !~ /\%\d+d/) {
	$pattern = "${pattern}_\%0${places}d.jpg";
}

@files = @ARGV;
usage unless @files;

for(@files) {
	my $new = sprintf($pattern, $num);
	warn "'$_' => '$new'\n";
	rename($_, $new) unless $fake;
	$num++;
}