473,597 Members | 2,174 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 2254
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
2967
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 item level: <Order> <Header> ... other header level stuff ... <Address>
3
1416
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 these two versions. A diff tool (like WinMerge, excellent) produces too many differences (spaces, new lines, comments, real changes, renaming, ...). Now I am using text processors to remove all trivial differences before going into WinMerge :...
21
11065
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 >= current time and <= (current time + n minutes)
41
3927
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 Y contains all the elements that are B but not in A. Z will then have the elements that are in both A and B. One way of doing this is of course to iterate throug the lists and compare each of the element, but is there a more efficient way? ...
11
7250
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 comparison in the command window, they're not equal. I'm pretty stumped on this one. Please help. I've tried the following structures, all with the same result: guid1.equals(guid2)
19
2637
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 = Color.Empty then..... or if mycolor is Color.Empty then .......
12
25981
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
3375
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 checkInteger(&$value, $checks) { $err = ''; if (!is_numeric($value) || (floatval($value) != intval($value))) { $err .= 'Input must be an integer. ';
17
8629
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 ? thanks for any help.
0
7962
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7884
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8267
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8380
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8024
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8258
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6681
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3880
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1493
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.