-
Notifications
You must be signed in to change notification settings - Fork 3
/
renumber.pl
executable file
·87 lines (65 loc) · 1.92 KB
/
renumber.pl
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/perl -w
#print "$#ARGV\n"; #Number of arguments-1
$prefix = "";
$start = "001";
# Do we have any arguments?
if( $#ARGV >= 0 ) {
# Ohh yes.... Process it...
foreach $arg (@ARGV) {
if( $arg eq '--help' || $arg eq '-h' || $arg eq '?' ) {
&help();
} elsif( $arg =~ /^[a-zA-Z]/ ) {
$prefix = "$arg - ";
} elsif( $arg =~ /^[0-9]/ ) {
$start = $arg;
} elsif( $arg =~ /^\// | $arg =~ /^\./ ) {
$DEST = $arg;
}
}
} else {
&help();
}
exit(1) if(! $DEST);
# -------------------------
open(LIST, "find . -maxdepth 1 -type f \| sort |");
while(! eof(LIST) ) {
$path = <LIST>;
chomp($path);
@entry = split(/\//, $path);
# Get filename...
$file = $entry[length(@entry)];
# Get the extension...
@TMP = split('\.', $file);
$len = scalar(@TMP);
$EXT = $TMP[$len-1];
$EXT = lc($EXT);
$EXT = 'jpg' if($EXT =~ /jpeg/);
&find_free_number() if(! $found_free_number);
next if( $file =~ /^$DEST\/$prefix/ );
next if( $file =~ /www_not_browsable/ );
$DESTINATION = "$DEST/$prefix$start.$EXT";
&move_file($entry[1], $DESTINATION);
$start++;
}
close(LIST);
sub move_file {
local($source, $destination) = @_;
printf( "Moving file: %-20s to %-5s\n",$source, $destination);
system( '/bin/mv', "-i", "$source", $destination );
}
sub find_free_number {
while( -f "$DEST/$prefix$start.$EXT" ) {
$start++;
}
$found_free_number = 1;
}
# -------------------------
sub help {
print "usage: renumber.pl <destination> [prefix] [start_no]\n";
print " Moves all files to <destination>/[prefix] - [startno].extension\n\n";
print "example: renumber.pl /tmp/files Pictures 001\n";
print " The first file will be called: /tmp/files/Pictures - 001.extension\n";
print " The second file will be called: /tmp/files/Pictures - 002.extension\n";
print " etcetera...\n";
exit( 0 );
}