Rob Dixon schreef:
> Nishi Bhonsle wrote:
>  >
>  > If I am still using the below logic, how can i modify the below
>  prog > to ignore empty directories and list only directories
>  containing > directories
>  > and files in buildlist.txt ?
>  >
>  > opendir DIR, $path or die "Can't open $path: $!";
>  >
>  >  #my @new = grep /[^.]/, readdir DIR;
>  >  my @new = grep { $_ ne "." and $_ ne ".." } readdir DIR;
>  >  closedir DIR;
>  >
>  >  open FILE,">>C:\buidlist.txt";
>  >  print FILE "$_\n" foreach @new;
>  >  close FILE;
>
> Hi Nishi
>
> To check whether a directory's empty or not you need to read /that/
> directory in turn. Since you'll be doing it twice it may as well go
> into a subroutine:
>
>    use strict;
>    use warnings;
>
>    my $path = 'C:\data';
>
>    my @new = grep { -d and dirlist($_) } dirlist($path);
>
>    open my $fh,">> C:\\buildlist.txt";
>    print $fh "$_\n" foreach @new;
>    close $fh;
>
>    sub dirlist {
>      my $path = shift;
>      opendir my $dh, $path or die "Can't open $path: $!";
>      my @dir = grep { $_ ne "." and $_ ne ".." } readdir $dh;
>      closedir $dh;
>      map "$path\\$_", @dir;
>    }


Variant:

#!perl
  use strict ;
  use warnings ;

  my $path = 'C:/data' ;

  my @new = grep { -d and dirlist($_) } dirlist($path) ;

  my $fh_name = 'C:/buildlist.txt' ;
  open my $fh, '>>', $fh_name
    or die  "open $fh_name, stopped $!" ;
  print $fh "$_\n" for @new, '-- ' ;
  close $fh
    or die "close $fh_name, stopped $!" ;

  sub dirlist
  {
    my $dh_name = shift ;
    opendir my $dh, $dh_name
      or die  "open $dh_name, stopped $!" ;
    my @dir = grep { $_ ne "." and $_ ne ".." } readdir $dh;
    closedir $dh
      or die "close $dh_name, stopped $!" ;
    map "$path/$_", @dir;
  }

-- 
Affijn, Ruud

"Gewoon is een tijger."



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to