in reply to Re: How do I put all sub-directories into an array?
in thread How do I put all sub-directories into an array?

Thanks

That's almost what I want but lists all the sub-directories below /home/www.

What I actually want is the third-level directories only i.e. the name_of_virtual_host directories only in the array, not all the directories below this level too.

My directory structure is /home/www/name_of_virtual_host/dir4/dir5/etc.

Can I use finddepth here to recurse down to the third level directories only?

Wylie
  • Comment on Re: Re: How do I put all sub-directories into an array?

Replies are listed 'Best First'.
Re: Re: Re: How do I put all sub-directories into an array?
by kodo (Hermit) on Jul 23, 2002 at 10:56 UTC
    well in this case I would simply do something like:
    opendir(DIR, '/home/www/'); while (my $dir = readdir(DIR)) { print $dir; next if $dir =~ /^\.\.?$/ || ! -d $dir; push(@dirs, $dir); } closedir(DIR);
    => Code is untested but should work.

    giant
      next if $dir =~ /^\.\.?$/ || ! -d $dir;
      Since you never fix up $dir to be the path including the directory name, this test is guaranteed to fail, except under the random moment when a directory below /home/www is also in the current directory. See the other solutions in this thread for working examples, of which yours is not.

      Code is untested but should work.
      Where "should" here means "I would like it to", not "it does". {grin}

      -- Randal L. Schwartz, Perl hacker

        hi merlyn,
        sorry I was in a hurry when writing this one, of course it would fail if /home/www issn't the current dir :)

        giant

      Giant, thanks for all your help with this.

      In the end I used a modification of mugwumpjism's suggestion to get to where I wanted to be.

      Here's the code:

      chdir "/home/www"; opendir DIR, "."; my @vhosts= ( # 1. This tests for the presence of directories grep { -d } ( # 2. this returns all directories but . & .. grep { !/^\.\.?$/ && -d } (readdir DIR) ) ); closedir DIR;
      This sticks all my virtual hosts in an array :).