File Search and contents scan with Perl

Posted on 2012-08-17
Last Modified: 2012-09-03
I have a list of file names in a text file and what I need to do is check whether the file exists in a directory and/or any of its sub-directories (the parent directory is remote if that makes a difference so I am using a path like '\\computer\share$\parent'. If the file exists I want to be able to read the file and look for certain string patterns using regular expressions. This has to be done in Perl but I am a complete novice!

This process will be running over a huge amount of files so needs to be quite efficient and close any unused resources if there are any... Any ideas?
Question by:Blowfelt82
    1 Comment
    LVL 25

    Accepted Solution

    This might help you get started:
    use strict;
    use File::Find;
    # Load all of the file names into a hash array so we can search the directories
    # once and check if it is a required file.
    my $wanted_file;
    while ( my $file_to_find = <DATA> ) {
        $wanted_file->{$file_to_find} = 1;
    # Search the directory and if the filename is one of the names loaded into the
    # hash array above - call a subroutine so it can be processed.
    my $remote_dir = '\\\\computer\share$\parent';
    find( sub { process_file($File::Find::name) if defined($wanted_file->{$_}) }, $remote_dir);
    # Found a file - it can be processed here.
    sub process_file($) {
        my ($file) = @_;
        print "Found a file ... ",$file,"\n";
    yet another file name.txt

    Open in new window

    I have used an in-line file for the file names - these can be read from an external file easily.

    What do you mean by a 'a huge amount of files'?  100's, 1000's, ... ?

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    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 …
    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…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    733 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