Hello

This is my first time posting despite having been around for awhile :) usually I get there with a problem I face in learning Perl, however, unfortunately I have come across a bit of a problem with checking and verifying MD5 of files in a directory and comparing with an MD5 file type of the same name in the same directory. Essentially the directory structure looks like this:

Directory - file, file.md5, file2, file2.md5, file3, file3.md5... etc

The code I've put together so far is limited but it has the ability to calculate the MD5 and write to a log if there is an error. I just don't know how to get the original MD5 and match it to the MD5 in the corresponding file. The code I've written so far is:

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 else { foreach my $item (@files) { my $filepath = "$sourcedirectory/$item"; &routine($filepath, $item); } } } sub routine { # identify filepath and name as parameters passed to routine my $file = shift; my $name = shift; } # 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 understand there is a big chunk missing in the 'routine' subroutine, but if any of you could give me some guidance on how to proceed with processing the file array returned by the getfiles subroutine, it would be greatly appreciated. If I am going the total wrong way with this, please point that out. Many thanks in advance!


In reply to 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.