Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing two directories using Perl
    text
    copied!<p>i am new to Perl so excuse my noobness,</p> <p>Here's what i intend to do. </p> <pre><code>$ perl dirComp.pl dir1 dir2 </code></pre> <p>dir1 &amp; dir2 are directory names.</p> <p>The script dirComp.pl should identify whether contents in dir1 &amp; dir2 are identical or not.</p> <p>I have come up with an algorithm</p> <pre><code>Store all the contents of dir1(recursively) in a list Store all the contents of dir2 in another list Compare the two list, if they are same - dir1 &amp; dir2 are same else not. my @files1 = readdir(DIR1h); my @files2 = readdir(DIR2h); # Remove filename extensions for each list. foreach my $item (@files1) { my ( $fileName, $filePath, $fileExt ) = fileparse($item, qr/\.[^.]*/); $item = $fileName; } foreach my $item (@files2) { my ( $fileName, $filePath, $fileExt ) = fileparse($item, qr/\.[^.]*/); $item = $fileName; } </code></pre> <p>I am not able to recursively traverse subdirectories in a given directory with the help of above code. Any help would be appreciated.</p> <p>EDIT: Using File:DirCompare</p> <pre><code>#!/usr/bin/perl -w use File::DirCompare; use File::Basename; if ($#ARGV &lt; 1 ) { &amp;usage; } my $dir1 = $ARGV[0]; my $dir2 = $ARGV[1]; File::DirCompare-&gt;compare($dir1,$dir2,sub { my ($a,$b) = @_; if ( !$b ) { printf "Test result:PASSED.\n"; printf "Only in %s : %s\n", dirname($a), basename($a); }elsif ( !$a ) { printf "Test result:PASSED.\n"; printf "Only in %s : %s\n", dirname($b), basename($b); }else { printf "Test result:FAILED.\n"; printf "Files $a and $b are different.\n"; } }); </code></pre> <p>I have a directory structure as below,</p> <pre><code>dir1/ dir2/ --file1.txt --file1.txt --file2.txt --file2.txt --file3.cpp --file3.cpp </code></pre> <p>I am facing Test result:FAILED. As the result must have been passed. Can anyone please correct me?</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload