Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Filename Template

by Abstraction (Friar)
on Aug 07, 2003 at 14:48 UTC ( [id://281910]=note: print w/replies, xml ) Need Help??


in reply to Filename Template

Has anyone any direction to point me in?

Sure, File::Find::Rule

Replies are listed 'Best First'.
Re: Re: Filename Template
by broquaint (Abbot) on Aug 07, 2003 at 15:00 UTC
    Further to that ...
    use File::Find::Rule; my $template = 'aaaa-nnnnnnnn'; (my $regex = $template) =~ s/([an])/$1 eq 'a' ? '[a-z]' : '[0-9]'/eig; my $matcher = rule( file => maxdepth => 1, name => qr/^$regex/i, start => $ARGV[0] ); while(my $file = $matcher->match) { ... }
    See. the File::Find::Rule docs for more info.
    HTH

    _________
    broquaint

Re: Re: Filename Template
by Limbic~Region (Chancellor) on Aug 07, 2003 at 15:06 UTC
    Abstraction,
    To expound on what you have said:
    #!/usr/bin/perl -w use strict; use File::Find::Rule; my @files = File::Find::Rule->file() ->name( qr/^[a-zA-Z]{4}-\d{8}/ ) ->in( / );
    It appeared at first in the docs that all you could do was file globs, but later on I saw a regex, so I think this will work.

    Cheers - L~R

    Update: Bah, broquaint beat me to it, so yes it will work. What's worse - his solution provides the ability to modify the template without modifying the regex. I guess the only thing I would have done differently is changed the character class to [a-zA-Z] instead of using /i for speed reasons - but if it runs a tad bit slower it just means you can go get more caffeine.

      even a simple readdir should do the trick

      #!/usr/bin/perl -w use strict; my $dir = '/some/dir'; my $file_re = qr/^(?:\.+|[a-zA-Z]{4}-[0-9]{8})$/; opendir(DIR, $dir) or die "opening $dir: $!\n"; for ( grep(!/$file_re/, readdir(DIR)) ) { # deal with non conforming file names here } closedir(DIR);

      use perl;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://281910]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (1)
As of 2024-04-18 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found