A couple of comments on your code ...

The readdir function iterates through each directory entry opened by opendir - Your code doesn't test whether the returned entry is either a file or directory and fails to iterate through any sub-directories present within the test directories. While not a problem per se, it does mean that great differences can exist between directories without being reported by your script.

Another issue lies with the order of code when calling stat on the returned directory entry, specifically the following code segment:

$values1 = stat($currentFile2) or warn "can't stat the file: $!\n"; $hashDir2{$currentFileCD} = { size =>$values1->size, mtime =>$values1->mtime, };

While a warning is generated when the file stat fails, your code continues through and still assigns return values from the failed stat to the directory hash. An alternate (more defensive) way to write this code would be:

if ( my $values = stat($currentFile2) ) { $hashDir2{ $currentFileCD } = { size => $values->size, mtime => $values->mtime } } else { warn "can't stat the file: $!\n" }

If you want to build a more robust file comparison tool that incorporates directory checking, you may want to look at the source to File::Find::Duplicates which builds an intermediate array of stat values for files found through File::Find.

On a much smaller and less significant note ... "This script will compare the same directory on two different machines for content, size and mtime" ... Where is the code comparing file content?

:-)

 

perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'


In reply to Re: Comparedirs.pl by rob_au
in thread Comparedirs.pl by RayRay459

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.