Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Perl File::Find alternative

Posted on 2016-11-15
1
Medium Priority
?
136 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 27

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
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…
Six Sigma Control Plans

971 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