Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Find file name

by blueapache (Acolyte)
on Nov 11, 2003 at 15:40 UTC ( [id://306219]=perlquestion: print w/replies, xml ) Need Help??

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

$Database = `find $db_path -type f -name *ofas.db` or die "Database for $system does not exist - $! \n";

this gives me the files I'm looking for including the path. How can I get rid of the path ?

Replies are listed 'Best First'.
Re: Find file name
by gjb (Vicar) on Nov 11, 2003 at 15:48 UTC

    File::Basename can help you out.

    Rather than using the shell's find, you might want to use File::Find.

    Hope this helps, -gjb-

Re: Find file name
by japhy (Canon) on Nov 11, 2003 at 15:48 UTC
    Why not use File::Find?
    use File::Find; my @database; find(\&ofas, $db_path); sub ofas { push @database, $_ if /ofas\.db$/; }

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Thanks. ...another newbie question How can I adjust this to deal with multiple results ?

        The above posted solution should already handle multiple results.

        ----
        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        : () { :|:& };:

        Note: All code is untested, unless otherwise stated

Re: Find file name
by broquaint (Abbot) on Nov 11, 2003 at 15:52 UTC
    Apply a dash of File::Spec and a sprig of File::Find::Rule and you'll find yourself with a most pleasing result
    use File::Spec; use File::Find::Rule; my @files = map { (File::Spec->splitpath $_)[-1] } find(file => name => "*ofas.db", in => $db_path);
    That will function in a similar fashion to your find command except you'll get back a list of filenames instead a string of newline seperated filepaths.
    HTH

    _________
    broquaint

      thanks, tried this but got Can't locate File/Find/Rule.pm in @INC guess that means its not available on our system. I'm new to this and I don't know how to tell what libraries/packages are installed
        If you've got privileged access to your machine then you can simply do
        perl -MCPAN -e 'install q[File::Find::Rule]'
        to install the module. However, if you're an unprivileged user you just need to set the path to somewhere that you can write to e.g
        shell> perl -MCPAN -e shell cpan shell -- CPAN exploration and modules installation (v1.65) ReadLine support enabled cpan> o conf makepl_arg PREFIX="/your/path/to/perl/lib"
        Or if you're installing from an unpacked tarball, just pass the PREFIX parameter to the Makefile.PL e.g
        perl Makefile.PL PREFIX=/your/path/to/perl/lib
        Then just add /your/path/to/perl/lib to @INC either through lib or the environment variable $PERL5LIB e.g
        use lib '/your/path/to/perl/lib'; use File::Find::Rule;
        HTH

        _________
        broquaint

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (None)
    As of 2024-04-25 00:47 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found