in reply to Checking MD5 of files in directory with corresponding MD5 file type

Im not sure how your md5 file is setup, in mine I just have a known md5 (one md5 per line, and no filenames because a known md5 should be sufficient enough to check against) and I loop through each file in a directory and get each files md5, then compare that against the md5's stored in the md5 file like so:
use warnings; use strict; use File::Slurp; my @md5_file = read_file("md5"); my $md5_string = join( "", @md5_file ); my $dirname = "path/to/files/to/be/checked"; while(my $file = (<'$dirname'/*>)) { open( my $fh, '<', $file ); binmode($fh); my $md5 = Digest::MD5->new->addfile($fh)->hexdigest; if ( $md5_string =~ $md5) { print "$md5 matches!\n"; } else { print "$md5 doesnt match ;(\n"; } }

Even though it looks like this code works as is, I am not sure if does and I dont have the time to test it myself right now, but hopefully it will help you a little bit on your way :)

Happy New Year!
  • Comment on Re: Checking MD5 of files in directory with corresponding MD5 file type
  • Download Code

Replies are listed 'Best First'.
Re^2: Checking MD5 of files in directory with corresponding MD5 file type
by james28909 (Deacon) on Dec 31, 2016 at 15:28 UTC

    To save from creating a completely new thread, I am going to ask a question here, under the code I posted, and hope someone knowledgable reads it.

    In the comparison operation 'if ( $md5_string =~ $md5 ){', would it be better to write it as 'if ( $md5 =~ $md5_string ){'

      Think about it, what do the strings contain? Will "md2" =~ "md1\nmd2\nmd3\n" match?

        Well, the $md5_string, as the variable name insists, contains thousands of md5s in a single string. To me it would make better sense for the lhs to be $md5 and rhs be $md5_string, which is saying "does this files $md5 match anywhere in the long list of md5's". So logically, I would assume it would be better written as 'if ( $md5 =~ /.*$md5_string.*/ ){' but in previous attempts to get it to work like that it failed. maybe it is because of how I were comparing. Ill go back and retry when I get home.