Greetings, monks.

I'm using File::Find in my current mini-project, and it's working well, but I'm having problems getting the full pathnames of the files it finds.

To start, my script takes from the command line a directory to pass to File::Find. This direcory can be a relative path.

I know that I can use the follow attribute to File::Find to get at the $fullname variable, but there may or may not be a symlink somewhere in the search path, which might muck up the results.

So, I tried using File::Spec and abs2rel, but am getting odd results if I pass a relative path. It'd be best to show my code and some output, so here's the relevant bits:

!/usr/bin/perl -w use strict; use Image::Size; use Data::Dumper; use File::Find; use File::Spec; use vars qw/*name *dir/; *name = *File::Find::name; *dir = *File::Find::dir; die "Usage: filecomp <path> <pattern>\n" unless (@ARGV == 2); my ($path, $pat) = @ARGV; my $qpat = qr/$pat/i; chomp (my $cwd = `pwd`); my $abspath = File::Spec->rel2abs($path, $cwd); print "Absolute base path is ",$abspath, "\n"; print "Location Filesize\n"; File::Find::find({wanted => \&wanted, # follow => 1 }, $path); sub wanted { /^$qpat/ && do { my $fullname = File::Spec->rel2abs($name, $abspath); printf "\n ------------- \n\$fullname is %s\n", $fullname; printf "\$name is %s\n", $name; --Results, with output from find to show that the files do exist-- [eggie@sunlink eggie]$ find tmp -name 'term*' -ls 4103 1 -rw------- 1 eggie eggie 160 Sep 2 16:52 tmp +/terminal.txt 675863 1 -rw------- 1 eggie eggie 160 Sep 3 14:35 tmp +/foo/terminal.txt [eggie@sunlink eggie]$ filecomp.pl tmp 'term.*' Absolute base path is /home/eggie/tmp Location Filesize ------------- $fullname is /home/eggie/tmp/tmp/terminal.txt $name is tmp/terminal.txt ------------- $fullname is /home/eggie/tmp/tmp/foo/terminal.txt $name is tmp/foo/terminal.txt
See the extra "tmp/" in the $fullname variable? Things look even uglier the deeper in the subdir that a file is found, something like /home/eggie/tmp/foo/bar/baz/foo/bar/baz/terminal.txt when it should be only /home/eggie/foo/bar/baz/terminal.txt Is this a bug/limitation in File::Spec? Do any of you know of a way around this, other than enabling follow in File::Find? (This is Perl, fer crying out loud, it should be possible.) Thanks in advance.

--

There are 10 kinds of people -- those that understand binary, and those that don't.


In reply to File::Find, File::Spec, and full paths. by mephit

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.