Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 2.12 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Comparing two directories using Perl
  2. $ perl dirComp.pl dir1 dir2
  3.        
  4. Store all the contents of dir1(recursively) in a list
  5. Store all the contents of dir2 in another list
  6. Compare the two list, if they are same - dir1 & dir2 are same else not.
  7.  
  8. my @files1 = readdir(DIR1h);
  9. my @files2 = readdir(DIR2h);
  10.  
  11.     # Remove filename extensions for each list.
  12.  
  13.         foreach my $item (@files1) {
  14.         my ( $fileName, $filePath, $fileExt ) = fileparse($item, qr/.[^.]*/);
  15.         $item = $fileName;
  16.         }
  17.  
  18.  
  19.         foreach my $item (@files2) {
  20.         my ( $fileName, $filePath, $fileExt ) = fileparse($item, qr/.[^.]*/);
  21.         $item = $fileName;
  22.         }
  23.        
  24. #!/usr/bin/perl -w
  25.  
  26. use File::DirCompare;
  27. use File::Basename;
  28.  
  29. if ($#ARGV < 1 )
  30. {
  31.         &usage;
  32. }
  33.  
  34. my $dir1 = $ARGV[0];
  35. my $dir2 = $ARGV[1];
  36.  
  37. File::DirCompare->compare($dir1,$dir2,sub {
  38.         my ($a,$b) = @_;
  39.         if ( !$b )
  40.         {
  41.                 printf "Test result:PASSED.n";
  42.                 printf "Only in %s : %sn", dirname($a), basename($a);
  43.         }elsif ( !$a ) {
  44.                 printf "Test result:PASSED.n";
  45.                 printf "Only in %s : %sn", dirname($b), basename($b);
  46.         }else {
  47.                 printf "Test result:FAILED.n";
  48.                 printf "Files $a and $b are different.n";
  49.         }
  50. });
  51.        
  52. dir1/                  dir2/
  53.     --file1.txt            --file1.txt
  54.     --file2.txt            --file2.txt
  55.     --file3.cpp            --file3.cpp
  56.        
  57. use strict;
  58. use warnings;
  59. use feature qw(say);
  60. use Digest::MD5::File qw(file_md5_hex);
  61.  
  62. use File::Find;
  63.  
  64. use constant {
  65.     DIR_1 => "/usr/foo",
  66.     DIR_2 => "/usr/bar",
  67. };
  68.  
  69. my %dir_1;
  70. my %dir_2;
  71.  
  72. find ( sub {
  73.         if ( -f $File::Find::name ) {
  74.             $dir_1{$File::Find::name} = file_md5_hex($File::Find::name);
  75.         }
  76.         else {
  77.             $dir_1($file::Find::name} = "DIRECTORY!";
  78.         }
  79.     }, DIR_1);
  80.  
  81. find ( sub {
  82.         if ( -f $File::Find::name ) {
  83.             $dir_2{$File::Find::name} = file_md5_hex($File::Find::name);
  84.         }
  85.         else {
  86.             $dir_2($file::Find::name} = "DIRECTORY!";
  87.         }
  88.     }, DIR_2);
  89.        
  90. /usr/bar/my/file/is/here.txt
  91. /usr/foo/my/file/is/here.txt