-
Notifications
You must be signed in to change notification settings - Fork 34
/
auto_extensions.pl
executable file
·53 lines (40 loc) · 1.2 KB
/
auto_extensions.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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 30 May 2020
# https://github.com/trizen
# Automatically determine the mime type of files and add the corresponding file extensions.
# Usage:
# perl script.pl [dir]
use 5.020;
use autodie;
use warnings;
use List::Util qw(any);
use File::Find qw(find);
use File::MimeInfo::Magic qw(mimetype extensions);
use File::Basename qw(dirname basename);
use File::Spec::Functions qw(curdir catfile);
my $dir = $ARGV[0] // curdir();
find(
{
no_chdir => 1,
wanted => sub {
return 1 if not -f $_;
my $dirname = dirname($_);
my $basename = basename($_);
my @extensions = extensions(mimetype($_));
return 1 if not @extensions;
if (any { defined($_) and $basename =~ /\.\Q$_\E\z/ } @extensions) {
return 1; # already has extension -- skip
}
my $ext = $extensions[0] // return 1;
my $newfile = catfile($dirname, $basename . '.' . $ext);
if (-e $newfile) {
say ":: $newfile already exists...";
}
else {
say ":: Renaming: $_ -> $newfile";
rename($_, $newfile);
}
},
} => $dir
);