?
Solved

Perl File::Find alternative

Posted on 2016-11-15
1
Medium Priority
?
116 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 26

Accepted Solution

by:
wilcoxon earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans
Suggested Courses

762 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