Solved

Perl File::Find alternative

Posted on 2016-11-15
1
52 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

867 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

22 Experts available now in Live!

Get 1:1 Help Now