commit
This commit is contained in:
41
renamer.pl
Executable file
41
renamer.pl
Executable file
@@ -0,0 +1,41 @@
|
|||||||
|
#!/usr/bin/perl
|
||||||
|
# vi: set tabstop=4 shiftwidth=4 expandtab : #
|
||||||
|
# version: 2.20200726
|
||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
|
|
||||||
|
my @files = <*>;
|
||||||
|
my %new_files;
|
||||||
|
|
||||||
|
foreach my $file (@files) {
|
||||||
|
if ($file =~ m#\.(jpe?g|png|gif)$#) {
|
||||||
|
my ($file_mtime) = (stat($file))[9];
|
||||||
|
|
||||||
|
my $new_file = "$file_mtime";
|
||||||
|
my $suffix = 1;
|
||||||
|
while (exists $new_files{$new_file}) {
|
||||||
|
$new_file = "$file_mtime-$suffix";
|
||||||
|
$suffix = $suffix + 1;
|
||||||
|
}
|
||||||
|
$new_files{$new_file} = $file;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
my @sorted_keys = sort(keys(%new_files));
|
||||||
|
my $cc = 0;
|
||||||
|
foreach my $key (@sorted_keys) {
|
||||||
|
my $file = $new_files{$key};
|
||||||
|
$file =~ m#\.(.+)$#;
|
||||||
|
my $ext = $1;
|
||||||
|
my $new_file = "$key.$ext";
|
||||||
|
|
||||||
|
if (not ($new_file eq $file)) {
|
||||||
|
print "$file -> $new_file\n";
|
||||||
|
rename($file, $new_file) || die "Can't rename file '$file'! Cause: $!";
|
||||||
|
$cc = $cc + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
print "-------------------\n";
|
||||||
|
print "rename $cc files\n";
|
||||||
|
|
||||||
35
script.sh
35
script.sh
@@ -1,35 +0,0 @@
|
|||||||
#!bash
|
|
||||||
|
|
||||||
PERL_SCRIPT=$(cat <<'EOF'
|
|
||||||
@files = split(/:/, $ARGV[0]);
|
|
||||||
@new_files = ();
|
|
||||||
foreach $file (@files) {
|
|
||||||
if ($file =~ m#^\./.+\.(jpe?g|png|gif)$#) {
|
|
||||||
($mtime) = (stat($file))[9];
|
|
||||||
$ext = $1;
|
|
||||||
$new_file = "./$mtime.$ext";
|
|
||||||
if ($new_file eq $file) {
|
|
||||||
next;
|
|
||||||
}
|
|
||||||
while (grep $_ eq $new_file, @new_files) {
|
|
||||||
if (! defined $suffix) {
|
|
||||||
$suffix = "-1";
|
|
||||||
} else {
|
|
||||||
$suffix = $suffix - 1;
|
|
||||||
}
|
|
||||||
$new_file = "./$mtime$suffix.$ext";
|
|
||||||
}
|
|
||||||
push(@new_files, $new_file);
|
|
||||||
$file =~ s#([ \(\)])#\\$1#g;
|
|
||||||
print "mv -n \"$file\" $new_file\n";
|
|
||||||
undef $suffix;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$count = @new_files;
|
|
||||||
print "echo \"Renamed: $count\"\n";
|
|
||||||
EOF
|
|
||||||
)
|
|
||||||
|
|
||||||
renamer() {
|
|
||||||
find . -maxdepth 1 -type f -print | tr '\n' ':' | xargs -I{} -- perl -e "$PERL_SCRIPT" '{}' | xargs -n1 -I{} -- bash -c '{}'
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user