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 -
#!/usr/bin/perl
-
use strict;
-
my $tar = "/home/sharmagu/perl/A.tar";
-
my $dir1 = "/home/sharmagu/perl/A";
-
my $dir2 = "/home/sharmagu/perl/tmp/A";
-
my $tmpDir = "/home/sharmagu/perl/tmp";
-
-
#chdir $tmpDir || die ("Error: Cannot change the directory -- $! \n");
-
#system "/bin/tcsh -c 'tar -xvf $tar'";
-
#print "Untaring of file is done\n";
-
my $result;
-
my $str;
-
my @different;
-
my @missing;
-
&getDirContent($dir1,$dir2);
-
-
# to check whether the directory is readable or not
-
sub checkDir {
-
my $dir = shift;
-
my %dir_hash;
-
if(-d $dir){
-
opendir(DIR,$dir) || die ("Error: Cannot open directory $! \n");
-
foreach (readdir(DIR)) {
-
undef($dir_hash{$_});
-
}
-
closedir(DIR);
-
}else{
-
print "Directory $dir is not readable \n";
-
}
-
return(\%dir_hash);
-
}
-
-
# to check the type of the file whether its a link or a file or a directory
-
sub checkType {
-
my $file = shift;
-
my $str = shift;
-
if (( -f $file ) && ( -r $file )){
-
$str = "f";
-
}elsif ( -l $file ){
-
$str = "l";
-
}elsif ( -d $file ){
-
$str = "d";
-
}else{
-
print "Cannot determine!!\n";
-
}
-
print "$file is of type: $str\n";
-
return $str;
-
}
-
-
# to compare the two directories
-
sub compareDir {
-
my $dir_contents1 = shift;
-
my $dir_contents2 = shift;
-
my $key1;
-
my $key2;
-
foreach $key2 ( keys %{$dir_contents2} ) {
-
if(!exists $dir_contents1->{$key2}) {
-
push @missing,$key2;
-
}elsif (-d ("$dir1/$key1") && -d ("$dir2/$key2")) {
-
my $return = compareDir($dir1/$key1, $dir2/$key2);
-
}
-
}
-
}
-
-
# to get the contents of the directory
-
sub getDirContent {
-
$dir1 = shift;
-
$dir2 = shift;
-
my $str1;
-
my $str2;
-
my $dir_contents1 = checkDir($dir1);
-
my $dir_contents2 = checkDir($dir2);
-
foreach my $file_name ( keys %{$dir_contents1} ) {
-
next if($file_name =~ /^\.+$/);
-
my $file = $dir1 . "/" . $file_name;
-
$str1 = checkType($file);
-
}
-
-
foreach my $file_basename ( keys %{$dir_contents2} ) {
-
next if($file_basename =~ /^\.+$/);
-
my $file = $dir2 . "/" . $file_basename;
-
$str2 = checkType($file);
-
}
-
# if ( $str1 eq $str2 ) {
-
$result = compareDir($dir_contents1, $dir_contents2);
-
-
-
print "result is: $result\n";
-
}
-
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.
KevinADC 4,059
Recognized Expert Specialist
Something along these lines: - use strict;
-
use warnings;
-
use File::Find;
-
my $dir1 = '/home/sharmagu/perl/A';
-
my $dir2 = '/home/sharmagu/perl/tmp/A';
-
my %dir = ();
-
-
find(\&wanted, $dir1,$dir2);
-
-
sub wanted {
-
my $f = $File::Find::name;
-
$f =~ s/^(?:$dir1|$dir2)//o;
-
$dir{$f}++
-
}
-
-
foreach my $files (sort keys %dir) {
-
print "$files\n" if $dir{$files} == 1;
-
}
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.
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: - use strict;
-
use warnings;
-
use File::Find;
-
my $dir1 = '/home/sharmagu/perl/A';
-
my $dir2 = '/home/sharmagu/perl/tmp/A';
-
my %dir = ();
-
-
find(\&wanted, $dir1,$dir2);
-
-
sub wanted {
-
my $f = $File::Find::name;
-
$f =~ s/^(?:$dir1|$dir2)//o;
-
$dir{$f}++
-
}
-
-
foreach my $files (sort keys %dir) {
-
print "$files\n" if $dir{$files} == 1;
-
}
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.
KevinADC 4,059
Recognized Expert Specialist
OK, this might be more what you are wanting: - use strict;
-
use warnings;
-
use File::Find;
-
#use Data::Dumper;
-
my $dir1 = '/home/sharmagu/perl/A';
-
my $dir2 = '/home/sharmagu/perl/tmp/A';
-
my %dir = ();
-
-
find(\&wanted, $dir1,$dir2);
-
-
sub wanted {
-
my $f = $File::Find::name;
-
next if $File::Find::name =~ /^\.{1,2}$/;# remove this line to speed up the script, it probably is not necessay.
-
$f =~ s/^($dir1|$dir2)//o;
-
$dir{$1}{$f}++; # create a hash for each parent
-
$dir{$f}++; # count all files in both parents
-
}
-
-
foreach my $parent ($dir1, $dir2) {
-
print "Checking '$parent' for missing folders/files.....\n";
-
foreach my $file ( sort keys %{ $dir{$parent} } ) {
-
print " $file\n" if $dir{$file} == 1;
-
}
-
}
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: -
Checking ''/home/sharmagu/perl/A'' for missing folders/files.....
-
'blah.txt'
-
'folder/foo.dat'
-
Checking ''/home/sharmagu/perl/temp/A'' for missing folders/files.....
-
'file.txt'
-
'folder/test.dat'
-
so any file you see listed below the parent directory is in the other directory but not in the parent directory.
Dear Kevin,
It works.
Thanks a lot buddy!! :-)
Regards,
Gunjan
KevinADC 4,059
Recognized Expert Specialist
Dear Kevin,
It works.
Thanks a lot buddy!! :-)
Regards,
Gunjan
You're welcome. Merry Christmas.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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 =...
|
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;
...
|
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...
|
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 ?
...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
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...
| |