[prev in list] [next in list] [prev in thread] [next in thread] 

List:       perl-win32-users
Subject:    Re: readdir??
From:       "Jenda Krynicky" <Jenda () McCann ! cz>
Date:       2000-12-30 21:35:14
[Download RAW message or body]

> >     can someone help me? i want to check if a directory is
> > empty......how can i do that.
> 
> 
> $dir ="c:\\temp";
> @filenames = "";
> opendir (DIR, $dir);
> @filenames = grep (!/^\.\.?$/ , readdir (DIR));
> close DIR;  
> if (!@filenames){print "$dir is empty\n";}
> if (@filenames){print "$dir is not empty\n";}

Well ... this is inefficient. You read in all the file and 
subdirectory names just to see if there are any ...

what about :

$dir ="c:\\temp";

opendir (DIR, $dir);
{
 my ($tmp, $res) = ( undef, 0);
 while (defined ($tmp = readdir DIR)) {
  if ($tmp ne '.' and $tmp ne '..') {
   $res = 1;
   last;
  }
 } 
 if ($res) {
  print "$dir is empty\n";
 } else {
  print "$dir is not empty\n";
 }
}
close DIR;  

You read only until you find a file/directory other than '.' or '..'.

Jenda

== Jenda@McCann.cz == http://Jenda.McCann.cz ==
            FUCK THE ARMY!
                       Myself
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic