opendir and if -d $files problem

Discussion in 'Perl' started by Bob Gervais, Nov 7, 2003.

  1. Bob Gervais

    Bob Gervais Guest

    Hi,

    I am writing a very simple script that should return all
    subdirectories on a given path (non recursively, so just first level).

    This is the sub that I have for doing this but it gives very strange
    results:

    sub FindFiles
    {
    my ($dir) = @_ ;
    my @files ;

    opendir THISDIR, $dir or die "Error, cannot do opendir on $dir $!"
    ;
    my @allfiles = readdir THISDIR ;
    closedir THISDIR ;

    foreach my $file (@allfiles)
    {
    #if (-d $file)
    # {
    # print $file."\n";
    # };
    push @files, $file if (-d $file) ;
    }

    return (@files)
    }

    When I run this on the same path as the script is located ('.') it
    gives me the correct results.
    If I give a path along of where to check, it gives me a few of the
    subdirs (for example on the C: drive it would give me the TEMP, the
    WINDOWS and the RECYCLER directory, but not the PROGRAM FILES or some
    other dirs I have.

    I am using Perl 5.8 on a Windows XP machine.

    Can anyone shed some light on this strange problem?

    Thanx,

    Bob
     
    Bob Gervais, Nov 7, 2003
    #1
    1. Advertising

  2. Bob Gervais <> wrote:
    > opendir THISDIR, $dir or die "Error, cannot do opendir on $dir $!";
    > my @allfiles = readdir THISDIR ;


    [ snip ]

    > When I run this on the same path as the script is located ('.') it
    > gives me the correct results.


    The problem is that readdir() returns filenames, not full paths.

    my @subdirs = grep -d, map "$dir/$_", readdir(THISDIR);

    [ This group is defunct; please use clp.misc instead. ]

    --
    Steve
     
    Steve Grazzini, Nov 12, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew

    opendir()

    Andrew, Sep 3, 2003, in forum: Perl
    Replies:
    2
    Views:
    9,477
    John Bokma
    Sep 3, 2003
  2. Alex
    Replies:
    2
    Views:
    700
  3. Ramon
    Replies:
    5
    Views:
    10,837
    John C. Bollinger
    Jan 3, 2005
  4. theo22
    Replies:
    12
    Views:
    401
    Anno Siegel
    Jan 21, 2004
  5. it_says_BALLS_on_your forehead

    opendir and readdir vs glob iteration

    it_says_BALLS_on_your forehead, Dec 1, 2005, in forum: Perl Misc
    Replies:
    9
    Views:
    467
    robic0
    Dec 3, 2005
Loading...

Share This Page