#!/usr/bin/perl
# create a list of image files, mtime, and sha256sum for later analysis
use v5.12; # 5.12+ gives strict and say...
use warnings;
use File::Find;
use Digest::SHA;
use Image::Info qw(image_type);
use Term::ProgressBar::Simple;
use Text::CSV;
my @search_dirs = @ARGV or die "Need at least one directory name:$!";
my $csv = Text::CSV->new( { binary => 1, eol => qq{\r\n} } )
or die "Cannot use CSV: " . Text::CSV->error_diag();
my $dbname = q{fileinfo.db};
open my $FH, q{>:encoding(utf8)}, $dbname or die "$dbname: $!";
my $tot_files = 0;
find( sub { $tot_files++ if -f $File::Find::name }, @search_dirs );
say "Scanning $tot_files files in "
. join( q{ }, @search_dirs )
. ". Saving data to $dbname.";
my $progress = Term::ProgressBar::Simple->new($tot_files);
find( \&process_file, @search_dirs );
sub process_file {
return if not -f $File::Find::name;
$progress++;
if ( not image_type($File::Find::name)->{error} ) {
my $sha = Digest::SHA->new(q{sha256});
$sha->addfile( $File::Find::name, q{b} );
my (
$dev, $ino, $mode, $nlink, $uid,
$gid, $rdev, $size, $atime, $mtime
) = stat($File::Find::name);
$csv->print( $FH,
[ $File::Find::name, $_, $mtime, $size, $sha->hexdigest ] );
} # if it's an image
} # sub process_file
__END__
Comments