-
Notifications
You must be signed in to change notification settings - Fork 33
/
unidec_renamer.pl
executable file
·81 lines (64 loc) · 1.88 KB
/
unidec_renamer.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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 29 October 2012
# Edit: 23 June 2013
# https://github.com/trizen
# Unidecode filename renamer.
# Ex: fișier.mp3 -> fisier.mp3
# Usage: unidec_renamer.pl -r <dirs>
use utf8;
use 5.005;
use strict;
use warnings;
use File::Find qw(find);
use Getopt::Std qw(getopts);
use File::Basename qw(basename);
use Text::Unidecode qw(unidecode);
use File::Spec::Functions qw(catfile catdir splitdir);
my %opts;
getopts('r', \%opts);
my @dirs = grep { -d } @ARGV;
@dirs || die "usage: $0 [-r] <dir>\n";
binmode(STDOUT, ':utf8');
sub unidec_rename_file {
my ($filename, $new_filename) = @_;
if (not -e $new_filename) {
rename $filename, $new_filename
or do { warn "Can't rename: $!\n"; return };
}
else {
warn "'$new_filename' already exists! Skipping...\n";
}
return 1;
}
my @dirs_for_rename;
find {
no_chdir => 1,
wanted => sub {
my $filename = basename($File::Find::name);
utf8::decode($filename);
my $new_name = unidecode($filename);
if ($filename ne $new_name) {
my $dir = $File::Find::dir;
utf8::decode($dir);
print "[", qw(DIR FILE) [-f $_], "] $filename -> $new_name\n";
my $new_filename = (-f _) ? catfile($dir, $new_name) : do {
push @dirs_for_rename, [$_, ($dir eq $filename ? $new_name : catdir($dir, $new_name))];
return;
};
if ($opts{r}) {
unidec_rename_file($_ => $new_filename);
}
}
},
} => @dirs;
if ($opts{r}) {
foreach my $array_ref (
map { $_->[1] }
sort { $b->[0] <=> $a->[0] }
map { [scalar(splitdir($_->[0])), $_] } @dirs_for_rename
) {
unidec_rename_file($array_ref->[0], $array_ref->[1]);
}
}