Solved

Perl File::Find alternative

Posted on 2016-11-15
1
23 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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now