joggi has asked for the wisdom of the Perl Monks concerning the following question:

I tried defined </TMP/*> or print "empty TMP\n"; and if(`du -s ./TMP`>4) print "not empty\n"; is there any better way?
  • Comment on How do I determine whether a folder is empty?

Replies are listed 'Best First'.
Re: How do I determine whether a folder is empty?
by perlplexer (Hermit) on May 21, 2003 at 13:34 UTC
    # Returns: # 1 - empty # 0 - not empty # -1 - doesn't exist # Definition of "empty" -- no files/folders/links except . and .. sub isEmpty{ my ($dir) = @_; my $file; if (opendir my $dfh, $dir){ while (defined($file = readdir $dfh)){ next if $file eq '.' or $file eq '..'; closedir $dhf; return 0; } closedir $dfh; return 1; }else{ return -1; } }
    --perlplexer
      ++ ... but ...

      # -1 - doesn't exist or # you don't have permission to open or read the dir

      -derby

      sorry, couldn't resist =]

      #!/usr/bin/perl use strict; use warnings; sub isEmpty { opendir(DIR,shift) or die $!; my @files = grep { !m/\A\.{1,2}\Z/} readdir(DIR); closedir(DIR); @files ? 0 : 1; } print isEmpty('./dir') ? "empty\n" : "not empty\n";

      cp
      ----
      "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."
Re: How do I determine whether a folder is empty?
by rob_au (Abbot) on May 21, 2003 at 13:36 UTC
    Short, simple and direct using the glob function ...
    sub is_empty { ! ( () = glob $_[0] . '/*' ); }

    Note however that this approach employing glob will not include dot-files in the list used to determine whether the directory is empty - Thanks ++broquaint! If this is important, this code could be rewritten using the opendir, readdir and closedir sequence of commands, such as provided by perlplexer above.

    Updated as per no_slogan's comments below.

     

    perl -le 'print+unpack("N",pack("B32","00000000000000000000001001011011"))'

      This won't work, because glob called in scalar context wants to iterate through all the files in a directory, and won't even look at its argument until it finishes. It will remember what directory it was reading on a previous call to is_empty. You could call glob in list context like this:

      ! (() = glob $_[0] . '/*');
•Re: How do I determine whether a folder is empty?
by merlyn (Sage) on May 21, 2003 at 19:26 UTC
    That first test puts glob in a scalar context. Each time it is called, it'll return the next item of the directory, until it returns undef. Thus, you'll get inconsistent results.

    Don't use glob in a scalar context unless you plan to drain it each time in a loop.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: How do I determine whether a folder is empty?
by jacques (Priest) on May 21, 2003 at 15:44 UTC
    grep? egrep? find? ls? There's a bunch of tools you could use. You can use wildcards to filter the results. Be creative and keep it simple. There's no need to do this with more than a single line of code.
A reply falls below the community's threshold of quality. You may see it by logging in.