Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Perl

Idiom #273 Check if folder is empty

Set the boolean b to true if the directory at filepath p is empty (i.e. doesn't contain any other files and directories)

opendir(my $dh, $p) || die($!);
my $b = scalar(grep { !/^[\.]{1,2}$/ } readdir($dh)) ? 1 : 0;
closedir($dh);

Technically, you can omit the ternary operator since any non-zero value will evaluate as true.
import 'dart:io';
var b = await Directory(p).list().isEmpty;

New implementation...
< >
programming-idioms.org