This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View file

@ -0,0 +1,3 @@
Starting with a path to some directory, determine whether the directory is empty.
An empty directory contains no files nor subdirectories. With [[Unix]] or [[Windows]] systems, every directory contains an entry for “<code>.</code>” and almost every directory contains “<code>..</code>” (except for a root directory); an empty directory contains no other entries.

View file

@ -0,0 +1,24 @@
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Directories;
procedure EmptyDir is
function Empty (path : String) return String is
use Ada.Directories;
result : String := "Is empty.";
procedure check (ent : Directory_Entry_Type) is begin
if Simple_Name (ent) /= "." and Simple_Name (ent) /= ".." then
Empty.result := "Not empty";
end if;
end check;
begin
if not Exists (path) then return "Does not exist.";
elsif Kind (path) /= Directory then return "Not a Directory.";
end if;
Search (path, "", Process => check'Access);
return result;
end Empty;
begin
Put_Line (Empty ("."));
Put_Line (Empty ("./empty"));
Put_Line (Empty ("./emptydir.adb"));
Put_Line (Empty ("./foobar"));
end EmptyDir;

View file

@ -0,0 +1,17 @@
IF FNisdirectoryempty("C:\") PRINT "C:\ is empty" ELSE PRINT "C:\ is not empty"
IF FNisdirectoryempty("C:\temp") PRINT "C:\temp is empty" ELSE PRINT "C:\temp is not empty"
END
DEF FNisdirectoryempty(dir$)
LOCAL dir%, sh%, res%
DIM dir% LOCAL 317
IF RIGHT$(dir$)<>"\" dir$ += "\"
SYS "FindFirstFile", dir$+"*", dir% TO sh%
IF sh% = -1 ERROR 100, "Directory doesn't exist"
res% = 1
REPEAT
IF $$(dir%+44)<>"." IF $$(dir%+44)<>".." EXIT REPEAT
SYS "FindNextFile", sh%, dir% TO res%
UNTIL res% == 0
SYS "FindClose", sh%
= (res% == 0)

View file

@ -0,0 +1,40 @@
#include <stdio.h>
#include <dirent.h>
#include <string.h>
int dir_empty(const char *path)
{
struct dirent *ent;
int ret = 1;
DIR *d = opendir(path);
if (!d) {
fprintf(stderr, "%s: ", path);
perror("");
return -1;
}
while ((ent = readdir(d))) {
if (!strcmp(ent->d_name, ".") || !(strcmp(ent->d_name, "..")))
continue;
ret = 0;
break;
}
closedir(d);
return ret;
}
int main(int c, char **v)
{
int ret = 0, i;
if (c < 2) return -1;
for (i = 1; i < c; i++) {
ret = dir_empty(v[i]);
if (ret >= 0)
printf("%s: %sempty\n", v[i], ret ? "" : "not ");
}
return 0;
}

View file

@ -0,0 +1,7 @@
fs = require 'fs'
is_empty_dir = (dir) ->
throw Error "#{dir} is not a dir" unless fs.statSync(dir).isDirectory()
# readdirSync does not return . or ..
fns = fs.readdirSync dir
fns.length == 0

View file

@ -0,0 +1,12 @@
import std.stdio, std.file;
void main() {
auto dir = "somedir";
writeln(dir ~ " is empty: ", dirEmpty(dir));
}
bool dirEmpty(string dirname) {
if (!exists(dirname) || !isDir(dirname))
throw new Exception("dir not found: " ~ dirname);
return dirEntries(dirname, SpanMode.shallow).empty;
}

View file

@ -0,0 +1,27 @@
package main
import (
"fmt"
"io/ioutil"
"log"
)
func main() {
empty, err := IsEmptyDir("/tmp")
if err != nil {
log.Fatalln(err)
}
if empty {
fmt.Printf("/tmp is empty\n")
} else {
fmt.Printf("/tmp is not empty\n")
}
}
func IsEmptyDir(name string) (bool, error) {
entries, err := ioutil.ReadDir(name)
if err != nil {
return false, err
}
return len(entries) == 0, nil
}

View file

@ -0,0 +1,9 @@
import System.Directory (getDirectoryContents)
import System.Environment (getArgs)
isEmpty x = getDirectoryContents x >>= return . f . (== [".", ".."])
where f True = "Directory is empty"
f False = "Directory is not empty"
main = getArgs >>= isEmpty . (!! 0) >>= putStrLn

View file

@ -0,0 +1,5 @@
$ mkdir 1
$ ./isempty 1
Directory is empty
$ ./isempty /usr/
Directory is not empty

View file

@ -0,0 +1,15 @@
procedure main()
every dir := "." | "./empty" do
write(dir, if isdirempty(dir) then " is empty" else " is not empty")
end
procedure isdirempty(s) #: succeeds if directory s is empty (and a directory)
local d,f
if ( stat(s).mode ? ="d" ) & ( d := open(s) ) then {
while f := read(d) do
if f == ("."|"..") then next else fail
close(d)
return s
}
else stop(s," is not a directory or will not open")
end

View file

@ -0,0 +1,5 @@
import java.nio.file.Paths;
//... other class code here
public static boolean isEmptyDir(String dirName){
return Paths.get(dirName).toFile().listFiles().length == 0;
}

View file

@ -0,0 +1,8 @@
function x = isEmptyDirectory(p)
if isdir(p)
f = dir(p)
x = length(f)>2;
else
error('Error: %s is not a directory');
end;
end;

View file

@ -0,0 +1,16 @@
$dir = 'path_here';
if(is_dir($dir)){
//scandir grabs the contents of a directory and array_diff is being used to filter out .. and .
$list = array_diff(scandir($dir), array('..', '.'));
//now we can just use empty to check if the variable has any contents regardless of it's type
if(empty($list)){
echo 'dir is empty';
}
else{
echo 'dir is not empty';
}
}
else{
echo 'not a directory';
}

View file

@ -0,0 +1 @@
sub dir_is_empty {!<$_[0]/*>}

View file

@ -0,0 +1,2 @@
use IO::Dir;
sub dir_is_empty { !grep !/^\.{1,2}\z/, IO::Dir->new(@_)->read }

View file

@ -0,0 +1 @@
(prinl "myDir is" (and (dir "myDir") " not") " empty")

View file

@ -0,0 +1,5 @@
import os;
if os.listdir(raw_input("directory")):
print "not empty"
else:
print "empty"

View file

@ -0,0 +1,2 @@
#lang racket
(empty? (directory-list "some-directory"))

View file

@ -0,0 +1,5 @@
# Checks if a directory is empty, but raises SystemCallError
# if _path_ is not a directory.
def empty_dir?(path)
not Dir.foreach(path).detect {|f| f != '.' and f != '..'}
end

View file

@ -0,0 +1,8 @@
# Checks if a directory is empty, but raises SystemCallError
# if _path_ is not a directory.
def empty_dir?(path)
Dir.foreach(path) {|f|
return false if f != '.' and f != '..'
}
return true
end

View file

@ -0,0 +1,4 @@
import java.io.File
def isDirEmpty(file:File) : Boolean =
return file.exists && file.isDirectory && file.list.isEmpty

View file

@ -0,0 +1,6 @@
proc isEmptyDir {dir} {
# Get list of _all_ files in directory
set filenames [glob -nocomplain -tails -directory $dir * .*]
# Check whether list is empty (after filtering specials)
expr {![llength [lsearch -all -not -regexp $filenames {^\.\.?$}]]}
}