473,503 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

comparing the given directory structures

4 New Member
Hello,

i want to compare the two directory structures and find out if any file or directory is missing in the second directory structure or not.
(directory1 is a super set of directory2)
I have written some code but i am confused with where exaclty the recursive call should be made and what all should be passed as directory name changes at each level.

Please go through it and suggest me something i am stuck here. Please
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. my $tar = "/home/sharmagu/perl/A.tar";
  4. my $dir1 = "/home/sharmagu/perl/A";
  5. my $dir2 = "/home/sharmagu/perl/tmp/A";
  6. my $tmpDir = "/home/sharmagu/perl/tmp";
  7.  
  8. #chdir $tmpDir || die ("Error: Cannot change the directory -- $! \n");
  9. #system "/bin/tcsh -c 'tar -xvf $tar'";
  10. #print "Untaring of file is done\n";
  11. my $result;
  12. my $str;
  13. my @different;
  14. my @missing;
  15. &getDirContent($dir1,$dir2);
  16.  
  17. # to check whether the directory is readable or not
  18. sub checkDir {
  19.     my $dir = shift;
  20.     my %dir_hash;
  21.     if(-d $dir){
  22.         opendir(DIR,$dir) || die ("Error: Cannot open directory $! \n");
  23.         foreach (readdir(DIR)) {
  24.             undef($dir_hash{$_});
  25.         }
  26.     closedir(DIR);
  27.     }else{
  28.     print "Directory $dir is not readable \n";
  29.     }
  30.     return(\%dir_hash);
  31. }
  32.  
  33. # to check the type of the file whether its a link or a file or a directory
  34. sub checkType {
  35.     my $file = shift;
  36.     my $str = shift;
  37.     if (( -f $file ) && ( -r $file )){
  38.     $str = "f";
  39.     }elsif ( -l $file ){
  40.     $str = "l";
  41.     }elsif ( -d $file ){
  42.         $str = "d";
  43.     }else{
  44.         print "Cannot determine!!\n";
  45.     }
  46.     print "$file is of type: $str\n";
  47.     return $str;
  48. }    
  49.  
  50. # to compare the two directories
  51. sub compareDir {
  52.     my $dir_contents1 = shift;
  53.     my $dir_contents2 = shift;
  54.     my $key1;
  55.     my $key2;
  56.      foreach $key2 ( keys %{$dir_contents2} ) {
  57.         if(!exists $dir_contents1->{$key2}) {
  58.             push @missing,$key2;
  59.         }elsif (-d ("$dir1/$key1") && -d ("$dir2/$key2")) {
  60.            my $return = compareDir($dir1/$key1, $dir2/$key2);
  61.         } 
  62.     }
  63. }
  64.  
  65. # to get the contents of the directory
  66. sub getDirContent {
  67.     $dir1 = shift;
  68.     $dir2 = shift;
  69.     my $str1;
  70.     my $str2;
  71.     my $dir_contents1 = checkDir($dir1);
  72.     my $dir_contents2 = checkDir($dir2);
  73.     foreach my $file_name ( keys %{$dir_contents1} ) {
  74.         next if($file_name =~ /^\.+$/);
  75.         my $file = $dir1 . "/" . $file_name;
  76.         $str1 = checkType($file);
  77.     }
  78.  
  79.     foreach my $file_basename ( keys %{$dir_contents2} ) {
  80.         next if($file_basename =~ /^\.+$/);
  81.         my $file = $dir2 . "/" . $file_basename;
  82.         $str2 = checkType($file);
  83.     }
  84. #    if ( $str1 eq $str2 ) {
  85.     $result = compareDir($dir_contents1, $dir_contents2);
  86.  
  87.  
  88.     print "result is: $result\n";    
  89. }
  90.  
Dec 21 '07 #1
6 2248
KevinADC
4,059 Recognized Expert Specialist
You probably should look into using the File::Find module, which can generate a lsit of file/folders in any directory, including sub folders.
Dec 21 '07 #2
KevinADC
4,059 Recognized Expert Specialist
Something along these lines:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. use File::Find;
  4. my $dir1 = '/home/sharmagu/perl/A';
  5. my $dir2 = '/home/sharmagu/perl/tmp/A';
  6. my %dir = ();
  7.  
  8. find(\&wanted, $dir1,$dir2);
  9.  
  10. sub wanted {
  11.    my $f = $File::Find::name;
  12.    $f =~ s/^(?:$dir1|$dir2)//o;
  13.    $dir{$f}++
  14. }
  15.  
  16. foreach my $files (sort keys %dir) {
  17.     print "$files\n" if $dir{$files} == 1;
  18. }
The print out should show files and folders only seen once, which would indicate they are not in both directories. If the directory structure is big, this could take a little while to run and print output. If there are only a few hundred folders/files it should run pretty quickly.
Dec 23 '07 #3
gunjan29484
4 New Member
Hi Kevin,

Thanks a lot!!
you have decreased my code to an extent. :-)
but its only showing the files and folders seen only once, can i have the parent directory name where the file is not present.
for example, if i have given dirA and dirB as command line arguments to the script and saving them to dir1 and dir2 then if my file is not present in dirA then it should explictly show the same.

thanks a ton for the efforts!!
Gunjan


Something along these lines:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. use File::Find;
  4. my $dir1 = '/home/sharmagu/perl/A';
  5. my $dir2 = '/home/sharmagu/perl/tmp/A';
  6. my %dir = ();
  7.  
  8. find(\&wanted, $dir1,$dir2);
  9.  
  10. sub wanted {
  11.    my $f = $File::Find::name;
  12.    $f =~ s/^(?:$dir1|$dir2)//o;
  13.    $dir{$f}++
  14. }
  15.  
  16. foreach my $files (sort keys %dir) {
  17.     print "$files\n" if $dir{$files} == 1;
  18. }
The print out should show files and folders only seen once, which would indicate they are not in both directories. If the directory structure is big, this could take a little while to run and print output. If there are only a few hundred folders/files it should run pretty quickly.
Dec 24 '07 #4
KevinADC
4,059 Recognized Expert Specialist
OK, this might be more what you are wanting:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. use File::Find;
  4. #use Data::Dumper;
  5. my $dir1 = '/home/sharmagu/perl/A';
  6. my $dir2 = '/home/sharmagu/perl/tmp/A';
  7. my %dir = ();
  8.  
  9. find(\&wanted, $dir1,$dir2);
  10.  
  11. sub wanted {
  12.    my $f = $File::Find::name;
  13.    next if $File::Find::name =~ /^\.{1,2}$/;# remove this line to speed up the script, it probably is not necessay.
  14.    $f =~ s/^($dir1|$dir2)//o;
  15.    $dir{$1}{$f}++; # create a hash for each parent
  16.    $dir{$f}++; # count all files in both parents
  17. }
  18.  
  19. foreach my $parent ($dir1, $dir2) {
  20.     print "Checking '$parent' for missing folders/files.....\n"; 
  21.     foreach my $file ( sort keys %{ $dir{$parent} } ) {
  22.         print "   $file\n" if $dir{$file} == 1; 
  23.     }
  24. }
output lists each parent directory and will display the files that are in the other directory but not in the parent directory. Output will be something like:

Expand|Select|Wrap|Line Numbers
  1. Checking ''/home/sharmagu/perl/A'' for missing folders/files.....
  2.    'blah.txt'
  3.    'folder/foo.dat'
  4. Checking ''/home/sharmagu/perl/temp/A'' for missing folders/files.....
  5.    'file.txt'
  6.    'folder/test.dat'
  7.  
so any file you see listed below the parent directory is in the other directory but not in the parent directory.
Dec 24 '07 #5
gunjan29484
4 New Member
Dear Kevin,

It works.
Thanks a lot buddy!! :-)

Regards,
Gunjan
Dec 26 '07 #6
KevinADC
4,059 Recognized Expert Specialist
Dear Kevin,

It works.
Thanks a lot buddy!! :-)

Regards,
Gunjan
You're welcome. Merry Christmas.
Dec 26 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2955
by: Doug | last post by:
I need to compare two "address" structures within a document, and perform some action if they are not equal. The XML document is a purchase order, with an address at both the header and line...
3
1407
by: Christophe Rhin | last post by:
I have two versions of the same C++ framework (> 200 000 lines). These two versions evolved apart during 2 years from the same starting point. My goal is two find "significant" differences between...
21
11031
by: Javier | last post by:
Hi I´ve a routine that will read date and times in a vector of strings ie: 30/02/2005 19:20 In some moment I´ll need to check if there are in vector a date like current date with a time...
41
3901
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and...
11
7217
by: Jeff | last post by:
Hi - I'm experiencing a strange problem when comparing 2 guids. In my trial, they're not equal. When I step through the (VB.NET) code, they are evaluated as equal, and when I enter the...
19
2620
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
12
25953
by: barcaroller | last post by:
Is it legal to compare the contents of two multi-field variables (of the same struct) using "==" and "!="? struct { int a; int b; } x,y; ...
2
3363
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
17
8616
by: junky_fellow | last post by:
Guys, Is it a good way to compare two structures for equality by using "memcmp" ? If not, what could be the problems associated with this ? And what is the correct method of doing this ? ...
0
7083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7456
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.