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

Thanks Corion... I think where I am struggling the most is how to process two files in tandem from the array... i.e. file and file.md5

What I am trying to achieve is to generate the MD5 of 'file' which already has been generated from an instrument prior to transfer and stored in a corresponding file called file.md5

Just how I get to work with these two files separately but concurrently from the rest of the array is what is failing me at the moment

  • Comment on Re^2: Checking MD5 of files in directory with corresponding MD5 file type

Replies are listed 'Best First'.
Re^3: Checking MD5 of files in directory with corresponding MD5 file type
by Corion (Patriarch) on Dec 30, 2016 at 10:45 UTC

    Why do you want to process both, "file" and "file.md5"?

    I think you want to do something different with "file" than you want to do with "file.md5".

    Also note that if you know "file", you also know "file.md5", and if "file.md5" is not found that is an error.

    You can blindly open "file.md5" without needing to verify its existence beforehand.

      The way I was looking at it was that I would process the MD5 of 'file' and then open 'file.md5' and hold its content in memory to compare to the processed MD5 of 'file' - if that makes sense... then if it matches move to the next file and corresponding .md5 file and if there is an error, send it to the logmd5 subroutine for logging

        hold its content in memory

        Assuming the *.md5files contain just the value (single line ?) then consider reading them all first and use a hash to store the results for comparison later, for example

        #!perl # test.pl use strict; use warnings; use Cwd; use Digest::MD5; use Data::Dump 'pp'; my $sourcedirectory = cwd(); # '//path/to/directory' # create test file with md5 value open my $io_handle,'<','test.pl' or die "$!"; my $md5 = Digest::MD5->new; $md5->addfile($io_handle); close $io_handle; open OUT,'>','test.md5' or die "$!"; print OUT $md5->hexdigest; close OUT; # read md5 values into a hash my $md5values = get_md5values($sourcedirectory); pp $md5values; sub get_md5values { my $sourcefiles = shift; opendir(DIR, $sourcefiles) or die "Can't open directory, $!"; my @md5_files = grep /\.md5$/,readdir(DIR); closedir(DIR); my %md5 = (); for my $filename (@md5_files){ my $count = 0; open MD5,'<',$filename or die "$!"; while (<MD5>){ chomp; $md5{$filename} .= $_; ++$count; } close MD5; print "$count lines read from $filename\n"; } return \%md5; }
        poj

        OK. So, I see six steps in your plan:

        1. process the MD5 of 'file'
        2. and then open 'file.md5'
        3. and hold its content in memory
        4. (to) compare to the processed MD5 of 'file'
        5. then if it matches move to the next file and corresponding .md5 file
        6. if there is an error, send it to the logmd5 subroutine for logging

        Which parts of these have you already implemented and which are missing?

        Maybe it helps to write these either as print statements or as comments in your code, and then replace them one by one with real code:

        sub check_md5 { my( $path, $item ) = @_; # process the MD5 of 'file' # open 'file.md5' # hold its content in memory # compare to the processed MD5 of 'file' # if it matches move to the next file and corresponding .md5 file # if there is an error, send it to the logmd5 subroutine for logging };

        What is the first item you can implement? Do that, then do the next item from the list that you can implement. Do that until either you've implemented everything or until you get stuck.