Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

get list of files matching part of file name

by doubledecker (Scribe)
on Jul 11, 2012 at 12:13 UTC ( #981099=perlquestion: print w/ replies, xml ) Need Help??
doubledecker has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks

any ideas of how to get list of files matching part of filename. ex: get filenames sample.txt or sample123.txt

use strict; use warnings; use File::Basename; use Data::Dumper; my @files = glob(".* *"); my $dir = '/home/test'; foreach my $file ( @files ) { next if ( $file eq '.' or $file eq '..' ); my @matches = glob("$dir/$file*"); if ( @matches ) { print Dumper(@matches); } }

This isn't doing what i expected. Any ideas are appreciated

Comment on get list of files matching part of file name
Download Code
Re: get list of files matching part of file name
by RichardK (Priest) on Jul 11, 2012 at 12:37 UTC

    you didn't say what you expect your code to do, so it difficult to offer any useful suggestions, other than glob('sample*.txt')

    Maybe my @files = glob (".* *") doesn't do what you think it does?

    see glob

      I used glob to list all the files in my current directory and using those list of files i'm trying to find out matching files in other directory

        A couple of points:

        (1) In the foreach loop, $file contains a filename together with its path and extension. To extract just the name part, you need something like:

        (my $basename = fileparse($file)) =~ s/ \. .* $ //x;

        which uses the fileparse function from File::Basename to extract the filename, then removes the extension (if any).

        (2) print Dumper(@matches); will print each match on a separate line. You would probably be better off with something like this:

        printf "'%s' matches: %s\n", $basename, join(', ', @matches);

        HTH,

        Athanasius <°(((><contra mundum

Re: get list of files matching part of file name
by zentara (Archbishop) on Jul 11, 2012 at 14:23 UTC
    Here is a File::Find solution
    #!/usr/bin/perl # non recursive case insensitive search down # thru current directory for pattern in filenames. use warnings; use strict; use File::Find; $|++; my $search = $ARGV[0] or die "need search pattern $!\n"; my $path = '.'; my $regex = qr/\Q$search\E/i; find (sub { #prevent recursion into subdirs my $n = ($File::Find::name) =~ tr!/!!; #count slashes in file return $File::Find::prune = 1 if ($n > 1); return if -d; # return unless (-f and -T ); # limit to text files print "$File::Find::name\n" if /$regex/; }, $path);

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: get list of files matching part of file name
by Anonymous Monk on Jul 11, 2012 at 16:15 UTC

    What a strange way to use glob(). You're better off with proper use of readdir():

    # open current directory for reading opendir my $dh, '.' or die; # iterate through the entries while (my $dir = readdir($dh)) { # your code here } closedir $dh;

    If you want to use glob() properly, you need to do glob("*/*sample*") to get every file in the current directory's immediate subdirectories with 'sample' in its name.

    There seems to be at least one real bug in your code: You aren't in /home/test when you do the glob.

Re: get list of files matching part of file name
by Anonymous Monk on Jul 11, 2012 at 17:02 UTC
    use File::Find::Rule; my @filenames = qw[ sample.txt sample123.txt ]; my $reFilenames = join '|', map quotemeta, @filenames; $reFilenames = qr/$reFilenames/i; my @files = find( 'file' => 'name' => $reFilenames, 'maxdepth' => 1, 'in' => $dir );

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://981099]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (15)
As of 2014-07-30 13:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (232 votes), past polls