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 2978 numberwhun 3,509
Recognized Expert Moderator Specialist
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::Rec ursive
.
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 or unix
filesystem? Thanks
|
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 - the path to the
corresponding file and call submit with the script.
Thank you.
|
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 of directories
/ sub-directories).
I am looking for a script that will recursively extract a single file
from each of these archives e.g. the file INSTALL, for the extracted
file to be moved to a different location and renamed to the name of
|
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 named "one", all directories that start with 02, move
to a directory name "two", and so on....
I can't seem to find any easy way to do this.
Looks like shutil.move only lets you move if the subdirectory DOES
NOT exist, so after the first...
| |
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 subdirectories in these two directories.
Suppose there are two folders OST1 and OST2 each containing two files ERM1 and ERM2... when i compare OST1 and OST2 i need to get a report of all the changes in the files ERM1 and ERM2 in addition to any changes in...
|
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') directly?
Thanks,
Fabian
|
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
Sub_directory
files
Sub_directory
files
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 would like to do
is recursively backup the specified directories (which copytree will
do), but be able to specify exclusion directories (which copytree does
not appear to allow you to do). My initial thoughts were I'll
probably have to use...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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: 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 we have to send another system
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |