Hi All,
I have done a script to compare 2 similar directories containing equal number of files, and to compare the content of those files in the directories.
But there is some problem in the recursion. Can anybody findout where I am going wrong?
Here is the code. -
#!/usr/bin/perl
-
-
sub dirparse{
-
-
our $scalar;
-
our @content1, @content2;
-
-
opendir DH1, $path1;
-
-
for $list1 (sort readdir(DH1)) {
-
print $list1,"\n";
-
-
if ($list1 ne "." && $list1 ne "..") {
-
if (-d $path1.$list1) {
-
print "Inside directory recursion\n";
-
print "List1 is :",$list1,"\n";
-
$path1 = $path1.$list1."/";
-
dirparse;
-
}
-
-
$pat1 = $path1.$list1;
-
$path1{$list1} = $pat1;
-
-
print "pat1 : ", $pat1,"\n";
-
-
@array1 = (@array1 , $list1);
-
}
-
}
-
-
opendir DH2, $path2;
-
-
for $list2 (sort readdir(DH2)) {
-
if ($list2 ne "." && $list2 ne "..") {
-
if (-d $path2.$list2 ) {
-
$path2 = $path2.$list2."/";
-
dirparse;
-
}
-
-
$pat2 = $path2.$list2;
-
$path2{$list2} = $pat2;
-
@array2 = (@array2, $list2);
-
}
-
}
-
-
$number1 = scalar @array1;
-
$number2 = scalar @array2;
-
-
print "Array 1 is ",@array1,"\n";
-
-
if ($number1 == $number2){
-
print "Equal number of files \n";
-
}
-
-
$number1 = $number1-1;
-
-
for (0..$number1) {
-
if($array1[$_] ne $array2[$_]) {
-
die "Two files are not same name \n";
-
}
-
-
#print $array1[$_],"\n";
-
#print %path1,"\n";
-
#print %path2, "\n";
-
-
if(-d $path1{$array1[$_]}) {
-
next;
-
}
-
-
print $path1{$array1[$_]};
-
-
open FILE1,$path1{$array1[$_]} or die "Can't open file: $_";
-
open FILE2,$path2{$array2[$_]} or die "Can't open file: $_";
-
-
@content1 = <FILE1>;
-
@content2 = <FILE2>;
-
-
print "For the file ", $array1[$_], " :";
-
-
print $_+1," : Files are compared:result :\n";
-
$con = scalar @content1;
-
for $scalar (0..$con) {
-
if (($content1[$scalar] cmp $content[$scalar]) != 0) {
-
print " Not Equal \n";
-
last;
-
}
-
}
-
if ($scalar == $con) {
-
print "Equal"
-
}
-
}
-
-
closedir(DH1);
-
closedir(DH2);
-
}
-
-
our $path1;
-
our $path2;
-
-
$path1 = "E:/perl/archana1/";
-
$path2 = "E:/perl/archana2/";
-
-
our %path1;
-
our %path2;
-
-
print "Hi : your directory matching is on process ...\n";
-
dirparse;
-
All suggestions are welcome as I am new to perl programming.
1 2962
Well, my first suggestion is that any time you want to do anything in Perl, make sure to check CPAN before writing your own code. (That is, unless you are like me and you like re-inventing the wheel for the purpose of learning). That aside though, in this case I would definitely searh cpan for a module.
If you do a search on cpan for "copy", you will find the File::Copy module. This is good, but doesn't help you with recursion. For that, you might want to read the module called File::Copy::Recursive
.
That module should do what you want to do as far as navigating a directory structure, you just have to code around it to do whatever else you need to do.
Regards,
Jeff
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ruby Tuesday |
last post by:
Is there a fast way to read files/directory recursively?
Instead of inspecting each file(s)/dir(s), is there a way to know that its a
file or a directory from its hidden attribut both for windows...
|
by: Trinity |
last post by:
Hi,
I want to delete a particular file in all the sub-directories. I am
using the SHFileOperation() API. But it is not deleting the files
recursively. Any sample code is appreciated.
TIA
|
by: Kamen TOMOV |
last post by:
Hi,
Is uploading recursively directories to a web server possible with
JavaScript?
I mean is it possible read a directory recursively and dynamically
construct <input type="file"> with value...
|
by: Peter Thorne |
last post by:
I am a perl newbie who is trying to write a script to automate a
task.
I have a large collection of compressed archives (mostly .tar.gz,
tar.bz2, tar.Z, .tgz etc). This are stored in a number...
|
by: DataSmash |
last post by:
Hi,
I need to organize thousands of directories full of files.
I want to move these directories into other subdirectories.
For example, all the directories that start with 01, move to
a directory...
|
by: Rahul83 |
last post by:
Hi..
I'm new to Perl programming. I need to compare two directories of similar structure... check all the subdirectories in it and generate a report of the changes in the files in all the...
|
by: Fabian Steiner |
last post by:
Hello!
As far as I can see os.chmod() doesn't adjust permissions on directories
recusively. Is there any other possibility to achieve this aim except for
calling os.system('chmod -R /dir')...
|
by: bhumikas |
last post by:
Hi all,
I need a help in perl script.The basic idea is,it must have command line arguments for the user flexibility.the files are in the format as shown below.
MainFolder
Directory
...
|
by: misceverything |
last post by:
I am writing a script that will backup specified folders from one hard
drive to another (for example, backup source "C:\DATA", destination "D:
\Backup"), and was thinking of using shutil. What I...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
| |