473,748 Members | 2,353 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bug in Script - Comparing two directories recursively

3 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. sub dirparse{
  4.  
  5.     our $scalar;
  6.     our @content1, @content2;
  7.  
  8.     opendir DH1, $path1;
  9.  
  10.     for $list1 (sort readdir(DH1)) {
  11.         print $list1,"\n";
  12.  
  13.         if ($list1 ne "." && $list1 ne "..") {
  14.             if (-d $path1.$list1) {
  15.                 print "Inside directory recursion\n";
  16.                 print "List1 is :",$list1,"\n";
  17.                 $path1 = $path1.$list1."/";
  18.                 dirparse;
  19.             }
  20.  
  21.             $pat1 = $path1.$list1;
  22.             $path1{$list1} = $pat1;
  23.  
  24.             print "pat1 : ", $pat1,"\n";
  25.  
  26.             @array1 = (@array1 , $list1);
  27.         }
  28.     }
  29.  
  30.     opendir DH2, $path2;
  31.  
  32.     for $list2 (sort readdir(DH2)) {
  33.         if ($list2 ne "." && $list2 ne "..") {
  34.             if (-d $path2.$list2 ) {
  35.                 $path2 = $path2.$list2."/";
  36.                 dirparse;
  37.             }
  38.  
  39.             $pat2 = $path2.$list2;
  40.             $path2{$list2} = $pat2;
  41.             @array2 = (@array2, $list2);
  42.         }
  43.     }
  44.  
  45.     $number1 = scalar @array1;
  46.     $number2 = scalar @array2;
  47.  
  48.     print "Array 1 is ",@array1,"\n";
  49.  
  50.     if ($number1 == $number2){
  51.         print "Equal number of files \n";
  52.     }
  53.  
  54.     $number1 = $number1-1;
  55.  
  56.     for (0..$number1) {
  57.         if($array1[$_] ne $array2[$_]) {
  58.             die "Two files are not same name \n";
  59.         }
  60.  
  61.         #print $array1[$_],"\n";
  62.         #print %path1,"\n";
  63.         #print %path2, "\n";
  64.  
  65.         if(-d $path1{$array1[$_]}) {
  66.             next;
  67.         }
  68.  
  69.         print $path1{$array1[$_]};
  70.  
  71.         open FILE1,$path1{$array1[$_]} or die "Can't open file: $_";
  72.         open FILE2,$path2{$array2[$_]} or die "Can't open file: $_";
  73.  
  74.         @content1 = <FILE1>;
  75.         @content2 = <FILE2>;
  76.  
  77.         print "For the file ", $array1[$_], " :";
  78.  
  79.         print $_+1," : Files are compared:result :\n";
  80.         $con = scalar @content1;
  81.         for $scalar (0..$con) {
  82.             if (($content1[$scalar] cmp $content[$scalar]) != 0) {
  83.                 print " Not Equal \n";
  84.                 last;
  85.             }
  86.         }
  87.         if ($scalar == $con) {
  88.             print "Equal"
  89.         }
  90.     }
  91.  
  92.     closedir(DH1);
  93.     closedir(DH2);
  94. }
  95.  
  96. our $path1;
  97. our $path2;
  98.  
  99. $path1 = "E:/perl/archana1/";
  100. $path2 = "E:/perl/archana2/";
  101.  
  102. our %path1;
  103. our %path2;
  104.  
  105. print "Hi : your directory matching is on process ...\n";
  106. dirparse;
  107.  
All suggestions are welcome as I am new to perl programming.
Jul 18 '07 #1
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
Jul 18 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3146
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
1
4113
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
3
1919
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.
1
6334
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
3
1869
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...
2
2949
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...
2
9888
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
9
2414
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
3
1957
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...
0
8991
marktang
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...
0
8831
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,...
0
9552
Oralloy
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...
0
9376
jinu1996
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...
1
6796
isladogs
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...
0
6076
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();...
0
4607
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...
1
3315
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
3
2215
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.