Last active
May 5, 2017 16:02
-
-
Save tsaiid/219a754485cc3767d9e888fc7dbdd59e to your computer and use it in GitHub Desktop.
Reverse filename order by a given range
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use File::Copy; | |
use utf8; | |
use strict; | |
my $usage = "Usage: $0 from to."; | |
if (not defined $ARGV[0] or not defined $ARGV[1]) { | |
die "$usage\n"; | |
} | |
my $from_no = $ARGV[0]; | |
my $to_no = $ARGV[1]; | |
if ($from_no !~ /^\d+$/ or $to_no !~ /^\d+$/ or $from_no >= $to_no) { | |
die "$usage\n"; | |
} | |
my $len = (length $from_no > length $to_no ? length $from_no : length $to_no); | |
if ($len < 2) { $len = 2; } | |
my $pattern = '%0'.$len.'d'; | |
my @files = <*>; | |
@files = sort @files; | |
my $range_pat; | |
for my $ii ($from_no .. $to_no) { | |
$range_pat .= (sprintf '%0'.$len.'d', $ii).'|'; | |
} | |
chop $range_pat; | |
@files = grep {m/.?($range_pat)\.jpg/i} @files; | |
my $i = 0; | |
my @files2; | |
for my $f (@files) { | |
my $rep = sprintf "%0".$len."d_", $to_no - ($i++); | |
$f =~ m/^(.*)(\d{$len})(\.jpg)$/i; | |
my $new_f = $1.$rep.$3; | |
push @files2, $new_f; | |
move($f, $new_f) || die "move: $!"; | |
} | |
$i = 0; | |
for my $f (@files2) { | |
#my $rep = sprintf "%0".$len."d", $from_no + ($i++); | |
$f =~ m/^(.*)(\d{$len})_(\.jpg)$/i; | |
my $new_f = $1.$2.$3; | |
move($f, $new_f) || die "move: $!"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment