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!
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |