mnlight has asked for the wisdom of the Perl Monks concerning the following question:

What Module do I use and How do I use it to find a list of files that have the same name but are numbered.
e.g.
db.table.01
db.table.02

I am not able to figure out how to use the Find::File mod to do this.
  • Comment on Find a list of numbered files with same basename

Replies are listed 'Best First'.
Re: Find a list of numbered files with same basename
by Roy Johnson (Monsignor) on Apr 22, 2005 at 14:28 UTC
    #!perl use strict; use warnings; use File::Find; my @found_list; sub wanted { push @found_list, $File::Find::name if /^db\.table\.\d+$/ } find \&wanted, '.'; print "$_\n" for @found_list;

    Caution: Contents may have been coded under pressure.
Re: Find a list of numbered files with same basename
by dragonchild (Archbishop) on Apr 22, 2005 at 14:22 UTC
      This looks like what I need. Thank you.
Re: Find a list of numbered files with same basename
by nimdokk (Vicar) on Apr 22, 2005 at 14:22 UTC
    You could try something like:

    $dir='/path/to/dir'; chdir $dir or die "Unable to change to $dir. $!"; @list=glob("db.table.??");
    Might have to change things depending on if all the files are in one directory or if you have to dig through directories. In that case, File::Find might be more helpful. I've found it slow, but the docs are pretty straightforward.
Re: Find a list of numbered files with same basename
by salva (Canon) on Apr 22, 2005 at 14:58 UTC
    if they are all on the same directory then you don't need File::Find, just use glob:
    my @file_names=glob 'db.table.[0-9][0-9]';
      Using glob makes it very simple to generalize this to find any pattern you want.
      my $pattern = "*.[0-9][0-9]"; my @file_names = glob $pattern;
      for instance, would find all files that ended with a double digit number.

      To bad the glob format isn't quite as nice as the full regular expression engine, I'd have liked to be able to do \d+, but without further complicating this, we can't.

      -Scott

        Why is this not returning anything? The files exit the format is correct but glob does not populate the array.
        sub find_files{ my ($dir, $db, $table) = @_; my $file = "$dir" ."/" . "$db.$table.[0-9][0-9]"; print "$file\n"; my @files = glob $file; foreach my $found_file (@files){ print "$found_file\n"; } return @files; }