Solved

Perl File::Find alternative

Posted on 2016-11-15
1
86 Views
Last Modified: 2016-11-15
Hi - I wrote the below code to find a file in a directory and later compare the MD5 values of those files.
The only problem I have is I used "File::Find::Rule" to find the file and now I don't want to use it because of limitations to the server I am deploying (need to install this module and i am not allowed to install).
so can some one tell me a alternate way for the below code snippet
my $ffr_obj = File::Find::Rule->file()
                            ->name($fName)
                           ->maxdepth($level)
                           ->start($compareDirectory);
if (my $compareFilePath = $ffr_obj->match())

Open in new window


My full code:
#!/usr/bin/perl

use 5.010;
use strict;
use warnings;
use File::Find::Rule;
use Digest::MD5 qw(md5_hex);
my $fpath = "/user/testUser/TestDate/testfile1.txt";

#here i provided file name but in general i will look for each file in /user/testUser/TestDate folder
my $fName  = "testfile1.txt";
my $compareDirectory="/user/testUser/compareDir";
my $level = shift // 2;
my $ffr_obj = File::Find::Rule->file()
                            ->name($fName)
                           ->maxdepth($level)
                           ->start($compareDirectory);
if (my $compareFilePath = $ffr_obj->match())
{
 print "compare file path: $compareFilePath \n";
 my $compareFileName = (split(/\//, $compareFilePath))[-1];
 print "compare File Name: $compareFileName \n";
 print "FILE FOUND: Matching file was found in compare Directory: $compareFileName\n";
 open (my $origFileRead, '<', $fpath) or die "Can't open file in '$fpath': $!";
 binmode ($origFileRead);
 open (my $compFileRead, '<', $compareFilePath) or die "Can't open file in:' $compareFilePath': $!";
 binmode ($compFileRead);
 my $originalFileHash = Digest::MD5->new->addfile($origFileRead)->hexdigest;
 my $compareFileHash = Digest::MD5->new->addfile($compFileRead)->hexdigest;
 
 if ($originalFileHash eq $compareFileHash) {
   print "MATCH: The MD5's match perfectly", "\n";          
 } 
 else {
   print "MATCH: The MD5's did not match", "\n";          
 }
}

Open in new window


Thanks,
0
Comment
Question by:shragi
1 Comment
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 41888464
I don't have time to rewrite the code right now but I'd suggest redoing it in File::Find which is a core module (so should be available on all servers).  I will try to take a look at it but the rest of this month is very busy for me.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl for loop for 2000 ms 7 109
perl split by | 2 111
batch script for automated email 12 135
PHP pack() : What exactly is meant by signed char and unsigned char? 7 128
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question