On Mon, Dec 19, 2011 at 8:08 PM, Jim Gibson <jimsgib...@gmail.com> wrote:
> On 12/19/11 Mon Dec 19, 2011 11:32 AM, "Jonathan Harris" > <jtnhar...@googlemail.com> scribbled: > > > Hi Perl Pros > > > > This is my first call for help > > > > I am a totally new, self teaching, Perl hopeful > > > > If my approach to this script is simply wrong, please let me know as it > > will help my learning! > > > > The script aims to: > > > > 1) Read in a directory either from the command line, or from a default > path > > 2) Produce a hash for future checksum > > A "hash" of what? File names (directory contents) or file contents? > > > 3) Write this (hex digest) to a separate file, in a sub directory of the > > parent, which has the same name and a .md5 extension > > Same name as the file or same name as the directory? > > > 4) Check the original file for its size > > 5) Add this data to the newly created file on a new line (in bytes) > > Will this file contain information for one file or many files? > > > > > I have a script that will do most of this, except for analysing the file > > size - I think that the file size being analysed may be the md5 object > > result as the same value is printed to each file > > Print out the file size returned by stat. Check if it is the same displayed > by the ls command. > > > > > I am running out of ideas and would appreciate any help you could give! > > I have tried using File::stat::OO and File::stat - but to no avail - I > > could be using them incorrectly! > > I am afraid I do not understand exactly what you are trying to accomplish. > I > can't tell from your program whether or not you will end up with one digest > file for the entire directory, or one digest file for each file in the > directory. > > > > > Many thanks in advance... > > > > Jon. > > > > Here are some details: > > > > System: > > Mac OSX 10.7.2 > > Perl version 5.12 > > > > Script: > > > > #!/usr/bin/perl > > # md5-test-3.plx > > use warnings; > > use strict; > > use Digest::MD5; > > > > my $filesize = 0; > > You should declare variables where they are needed and not before. > > > my $dir = shift || '/Users/jonharris/Documents/begperl'; > > opendir (DH, $dir) or die "Couldn't open directory: $!"; > > > > my $md5 = Digest::MD5->new; > > while ($_ = readdir(DH)) { > > You are better off using a scalar variable and not the default variable, > which can get reused and overwritten: > > while( my $file = readdir(DH) ) { > > > $md5->add($_); > > You are adding file names to a string to be "digested". Is that what you > want? Or do you want to calculate a digest for the contents of each file? > > I have not used Digest::MD5, but if you to calculate the digest for the > contents of each file, you want to create a new digest object, open the > file, and use the addfile() method, then hexdigest() for each file. > > > $filesize = (stat(DH))[7]; > > You are applying stat to the directory read handle. You want to fetch data > for the file (untested): > > my $filesize = (stat("$dir/$file"))[7]; > > Note that you must prefix the file name with its full path. > > > #### Is it necessary to put the following into a new loop? > > No. It makes no sense to have a one-iteration loop. > > > > > foreach ($_) { > > open FH, ">> /Users/jonharris/Documents/begperl/md5/$_.md5" or die $!; > > You are appending lines to a file with a name that is based on an existing > file. Why? > > > binmode(FH); > > There is no need to set the mode of the output file to binary. Both > hexdigest and the file size will be written in ascii characters, not binary > data. > > > print FH $md5->hexdigest, "\n", $filesize; > > } > > close FH; > > } > > close DH; > > print "\n$dir\n\n"; > > > > ####### > > > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > > Hi Jim Thanks for responding Here are some answers to your questions: > A "hash" of what? File names (directory contents) or file contents? The aim is to produce a digest for each file in the folder > Same name as the file or same name as the directory? Each file created will have the same filename as the original, but with the extension '.md5' > Will this file contain information for one file or many files? This file will contain information for only one file This is so that when looking at the treated directory, the end result will be: some_movie.mov some_movie.mov.md5 another_movie.mov another_movie.mov.md5 etc... > Print out the file size returned by stat. Check if it is the same displayed by the ls command. I really have! However, this is where I am coming unstuck > I am afraid I do not understand exactly what you are trying to accomplish. I can't tell from your program whether or not you will end up with one digest file for the entire directory, or one digest file for each file in the directory. The program creates one digest file for each file in the directory : ) > my $filesize = 0; > You should declare variables where they are needed and not before. Noted > my $md5 = Digest::MD5->new; > while ($_ = readdir(DH)) { > You are better off using a scalar variable and not the default variable, > which can get reused and overwritten: Noted > $filesize = (stat(DH))[7]; > You are applying stat to the directory read handle. You want to fetch data > for the file (untested): > my $filesize = (stat("$dir/$file"))[7]; This could well be where it's going wrong > open FH, ">> /Users/jonharris/Documents/begperl/md5/$_.md5" or die $!; > You are appending lines to a file with a name that is based on an existing > file. Why? This is what was requested in the original brief - to have the digest file with the same filename as the original, just with the extension '.md5' > binmode(FH); > There is no need to set the mode of the output file to binary. Both > hexdigest and the file size will be written in ascii characters, not binary > data. I did this as it was part of the example code on the Digest::MD5 page on CPAN If it's not necessary, I'll drop it from the script Thanks for your help, Jim. I'm going to have a look over the script and try to produce something new Bests Jon