aboutsummaryrefslogtreecommitdiff
path: root/renumfiles
blob: 28ff2b92d0185217bc8fa8cb8c77f90bd5b82fec (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
#!/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;
$pattern =~ s/\%d/%02d/;
if($pattern !~ /\%\d+d/) {
	$pattern = "${pattern}_\%02d.jpg";
}
shift;

@files = @ARGV;
usage unless @files;

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