-
Notifications
You must be signed in to change notification settings - Fork 33
/
collect_gifs.pl
70 lines (51 loc) · 1.61 KB
/
collect_gifs.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
#!/usr/bin/perl
# Collect and move GIF images into a specific directory, by scanning a given a directory (and its subdirectories) for GIF images.
use 5.036;
use File::Find qw(find);
use File::Copy qw(move);
use File::Path qw(make_path);
use File::Basename qw(basename);
use File::Spec::Functions qw(catfile curdir rel2abs);
use Getopt::Long qw(GetOptions);
my $use_exiftool = 0; # true to use `exiftool` instead of `File::MimeInfo::Magic`
sub is_gif ($file) {
if ($use_exiftool) {
my $res = `exiftool \Q$file\E`;
$? == 0 or return;
defined($res) or return;
return ($res =~ m{^MIME\s+Type\s*:\s*image/gif}mi);
}
require File::MimeInfo::Magic;
(File::MimeInfo::Magic::magic($file) // '') eq 'image/gif';
}
sub collect_gif ($file, $directory) {
my $dest = catfile($directory, basename($file));
if (-e $dest) {
warn "File <<$dest>> already exists...\n";
return;
}
move($file, $dest);
}
GetOptions('exiftool!' => \$use_exiftool,)
or die "Error in command-line arguments!";
my @dirs = @ARGV;
@dirs || die "usage: perl $0 [directory | files]\n";
my $directory = rel2abs("GIF images"); # directory where to move the files
if (not -d $directory) {
make_path($directory)
or die "Can't create directory <<$directory>>: $!";
}
if (not -d $directory) {
die "<<$directory>> is not a directory!";
}
find(
{
wanted => sub {
if (-f $_ and is_gif($_)) {
say ":: Moving file: $_";
collect_gif($_, $directory);
}
},
},
@dirs
);