Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
Select your favorite languages :
  • Or search :

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)

import 'dart:io';
var b = await Directory(p).list().isEmpty;
import "os"
dir, err := os.Open(p)
if err != nil {
	panic(err)
}
defer dir.Close()
_, err = dir.Readdirnames(1)
b := err == io.EOF
uses FileUtil;
with FindAllFiles(p, AllFilesMask, False) do
try
  b := DirectoryExists(p) and (Count = 0);
finally
  Free;
end;
opendir(my $dh, $p) || die($!);
my $b = scalar(grep { !/^[\.]{1,2}$/ } readdir($dh)) ? 1 : 0;
closedir($dh);
import os
b = os.listdir(p) == []
b = Dir.empty?(p)
use std::fs;
let b = fs::read_dir(p).unwrap().count() == 0;

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