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

Hello all,

I got the list of files using the file find operation.
Howerver when I try to open these files, the file open
command for some of them fails.
This is because the find command is not returning the full path of the
files. so if
the file is in a subdirectory, then the open fails because
I specify only the name of the file and not its
absolute/relative path.

Is there any way to get absolute paths of the files which
are returned by the find command ?

regards,
Abhishek.

Replies are listed 'Best First'.
Re: opening files after File::Find
by dws (Chancellor) on Sep 18, 2002 at 18:43 UTC
    Is there any way to get absolute paths of the files which are returned by the find command ?

    When it doubt, read the documentation. In this case the POD embedded in File::Find, which reads, in part

    $File::Find::name contains the complete pathname to the file.

      Another option, if you're enamoured of the implicit variables and have a suitably recent version of File::Find, is that you can change the behaviour of $_ to match that of $File::Find::name by using the no_chdir option.

      find( { wanted => sub { print }, no_chdir => 1, }, @paths );

          --k.


Re: opening files after File::Find
by davorg (Chancellor) on Sep 18, 2002 at 18:45 UTC

    Did you try reading the documentation for File::Find?

    $File::Find::dir is the current directory name,
    $_ is the current filename within that directory
    $File::Find::name is the complete pathname to the file.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: opening files after File::Find
by broquaint (Abbot) on Sep 18, 2002 at 18:45 UTC
    Is there any way to get absolute paths of the files which are returned by the find command ?
    Yup, you'll find it in the package variable $File::Find::name. For more info see the File::Find docs.
    HTH

    _________
    broquaint

(jeffa) Re: opening files after File::Find
by jeffa (Bishop) on Sep 18, 2002 at 18:56 UTC
    Yes there is ... you need to use $File::Find::fullname in conjunction with follow => 1. See the docs for more info, but this should get you started:
    perl -MFile::Find -le 'find { wanted => sub {print $File::Find::fullna +me}, follow => 1}, shift' dir_to_scan

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: opening files after File::Find
by sch (Pilgrim) on Sep 18, 2002 at 18:46 UTC

    I guess you're using $_ which holds the filename.

    If you look at the documentation for File::Find you'll see you should really use $File::Find::name

    Hope that help's

Re: opening files after File::Find
by defyance (Curate) on Sep 18, 2002 at 18:47 UTC
    Welll, I haven't dealt much with File::Find, but I found this in the docs, maybe it will help you out..

    There is a variable $File::Find::fullname which holds the absolute pathname of the file with all symbolic links resolved

    This was taken from the docs for File::Find

    -- Can't never could do anything, so give me and inch, I'll make it a mile.