<>In case anyone wants to know the "final" code I used - here it is ... thanks to Corion for a lot of guidance along to the way! A little tidy up still to be done...

use strict; use warnings; use Digest::MD5; my $sourcedirectory = '//path/to/directory'; &main(); sub main { # creating file array my @files = &getfiles($sourcedirectory); # removal of the two directory structures which form the start of +the file array my $rmdir = shift (@files); my $rmdir2 = shift (@files); # capture of a count of the number of files remaining in the array my $filecount = @files; # check to see if there are no files, if none, exit script if ($filecount == 0) { print "\nNo files to process\n\n"; exit; } # where there are files, the routine subroutine is called for each + item individually (except for md5 file types) else { foreach my $item (@files) { next if $item =~ /.md5/; my $filepath = "$sourcedirectory/$item"; &routine($filepath, $item); } } } sub routine { # identify filepath and name as parameters passed to routine my $file = shift; my $name = shift; # open file, set to binary mode, close and process MD5 open my $fh, '<', $file or die "Can't open file: $!"; binmode ($fh); my $md5tocompare = &processmd5($fh); close $fh or die "Can't close file successfully: $!"; # check file.md5 existence and open and read to memory open my $fh2, '<', "$file.md5" or next; my $originalmd5 = <$fh2>; close $fh2 or die "Can't close file successfully: $!"; # check parity print "MD5 is $md5tocompare against orignal MD5 of $originalmd5\n" +; next if $md5tocompare eq $originalmd5; &logmd5($md5tocompare, $name); } # subroutine to process the MD5 Hex value of a filehandle passed sub processmd5 { my $io_handle = shift; my $md5 = Digest::MD5->new; $md5->addfile($io_handle); my $value = $md5->hexdigest; return $value; } # logreport subroutine that receives a string message as an argument, +and processes the log entry sub logmd5 { # message, and definition of date/time variables to enable logging my $md5 = shift; my $md5filename = shift; my $md5file = "$sourcedirectory/Log.txt"; # opening of log and appending of md5 to the log open(LOG, '>>', $md5file) or die "Can't open logfile $!"; print LOG "ERROR: $md5filename does not match - $md5 is not the or +iginal checksum"; close LOG; } # a subroutine to enable definition of files to be processed sub getfiles { my $sourcefiles = shift; my @file_list; opendir(DIR, $sourcefiles) or die "Can't open directory, $!\n"; @file_list = readdir(DIR); closedir(DIR); return @file_list; }

I also have a script to generate the MD5 files if anyone needs it... just message me. Thanks!


In reply to Re: Checking MD5 of files in directory with corresponding MD5 file type by deedo
in thread Checking MD5 of files in directory with corresponding MD5 file type by deedo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.