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
[code=perl]
#!/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,$di r) || die ("Error: Cannot open directory $! \n");
foreach (readdir(DIR)) {
undef($dir_hash {$_});
}
closedir(DIR);
}else{
print "Directory $dir is not readable \n";
}
return(\%dir_ha sh);
}
# 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_contents 2} ) {
if(!exists $dir_contents1->{$key2}) {
push @missing,$key2;
}elsif (-d ("$dir1/$key1") && -d ("$dir2/$key2")) {
my $return = compareDir($dir 1/$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_contents 1} ) {
next if($file_name =~ /^\.+$/);
my $file = $dir1 . "/" . $file_name;
$str1 = checkType($file );
}
foreach my $file_basename ( keys %{$dir_contents 2} ) {
next if($file_basena me =~ /^\.+$/);
my $file = $dir2 . "/" . $file_basename;
$str2 = checkType($file );
}
# if ( $str1 eq $str2 ) {
$result = compareDir($dir _contents1, $dir_contents2) ;
print "result is: $result\n";
}
[/code]
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
[code=perl]
#!/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,$di r) || die ("Error: Cannot open directory $! \n");
foreach (readdir(DIR)) {
undef($dir_hash {$_});
}
closedir(DIR);
}else{
print "Directory $dir is not readable \n";
}
return(\%dir_ha sh);
}
# 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_contents 2} ) {
if(!exists $dir_contents1->{$key2}) {
push @missing,$key2;
}elsif (-d ("$dir1/$key1") && -d ("$dir2/$key2")) {
my $return = compareDir($dir 1/$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_contents 1} ) {
next if($file_name =~ /^\.+$/);
my $file = $dir1 . "/" . $file_name;
$str1 = checkType($file );
}
foreach my $file_basename ( keys %{$dir_contents 2} ) {
next if($file_basena me =~ /^\.+$/);
my $file = $dir2 . "/" . $file_basename;
$str2 = checkType($file );
}
# if ( $str1 eq $str2 ) {
$result = compareDir($dir _contents1, $dir_contents2) ;
print "result is: $result\n";
}
[/code]
Comment