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 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.
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 item level:
<Order>
<Header>
... other header level stuff ...
<Address>
|
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 :...
|
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)
|
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?
...
|
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)
| |
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 .......
|
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 checkInteger(&$value, $checks) {
$err = '';
if (!is_numeric($value) || (floatval($value) != intval($value))) {
$err .= 'Input must be an integer. ';
|
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.
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |