473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Bug in Script - Comparing two directories recursively

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 2962
numberwhun
3,509 Expert Mod 2GB
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
Jul 18 '07 #2

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

Similar topics

4
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...
1
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
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...
1
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...
3
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...
2
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...
2
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')...
9
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 ...
3
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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,...
0
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...
0
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,...
0
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...

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.