Empty directory: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 6:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">I fs:list_dir(input()).empty
print(‘empty’)
E
print(‘not empty’)</langsyntaxhighlight>
 
=={{header|8086 Assembly}}==
{{trans|MS-DOS}}
 
<langsyntaxhighlight lang="asm">; this routine attempts to remove the directory and returns an error code if it cannot.
 
mov ax,seg dirname ;load into AX the segment where dirname is stored.
Line 36:
 
 
dirname db "GAMES",0</langsyntaxhighlight>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Directories;
procedure EmptyDir is
Line 62:
Put_Line (Empty ("./emptydir.adb"));
Put_Line (Empty ("./foobar"));
end EmptyDir;</langsyntaxhighlight>
{{out}}
<pre>Not empty
Line 73:
This uses the "argc", "argv", "file is directory" and "get directory" procedures specific to Algol 68 G.
<br>Note the Algol 68 G interpreter processes the command line parameters before "-" so this example expects the directory names to follow "-".
<langsyntaxhighlight lang="algol68"># returns TRUE if the specified directory is empty, FALSE if it doesn't exist or is non-empty #
PROC is empty directory = ( STRING directory )BOOL:
IF NOT file is directory( directory )
Line 104:
print( ( argv( i ), " is ", IF is empty directory( argv( i ) ) THEN "empty" ELSE "not empty" FI, newline ) )
FI
OD</langsyntaxhighlight>
{{out}}
<pre>
Line 115:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">emptyDir?: function [folder]-> empty? list folder
 
print emptyDir? "."</langsyntaxhighlight>
 
{{out}}
Line 124:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="autohotkey">MsgBox % isDir_empty(A_ScriptDir)?"true":"false"
 
isDir_empty(p) {
Line 130:
return 0
return 1
}</langsyntaxhighlight>
{{out}}
<pre>false</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f EMPTY_DIRECTORY.AWK
BEGIN {
Line 173:
return(msg)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 183:
 
=={{header|BaCon}}==
<langsyntaxhighlight lang="bacon">FUNCTION check$(dir$)
 
IF FILEEXISTS(dir$) THEN
Line 200:
 
dir$ = "."
PRINT "Directory '", dir$, "'", check$(dir$)</langsyntaxhighlight>
 
{{out}}
Line 213:
*2 - input directory does not exist.
*3 - input not found.
<langsyntaxhighlight lang="dos">@echo off
if "%~1"=="" exit /b 3
set "samp_path=%~1"
Line 242:
:folder_not_found
echo Folder not found.
exit /b 2</langsyntaxhighlight>
{{Out|Sample Session}}
(Saved the Batch File as IsEmpty.Bat in C:\)
Line 274:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> 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
Line 290:
UNTIL res% == 0
SYS "FindClose", sh%
= (res% == 0)</langsyntaxhighlight>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <dirent.h>
#include <string.h>
Line 332:
 
return 0;
}</langsyntaxhighlight>Running it:<pre>
% mkdir stuff; ./a.out /usr/ ./stuff /etc/passwd
/usr/: not empty
Line 340:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.IO;
 
Line 359:
}
}
</syntaxhighlight>
</lang>
Running it:<pre>
Assume c:\temp exists and is not empty, c:\temp\empty exists and is empty
Line 370:
=={{header|C++}}==
{{libheader|Boost}}
<langsyntaxhighlight lang="cpp">
#include <iostream>
#include <boost/filesystem.hpp>
Line 387:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(require '[clojure.java.io :as io])
(defn empty-dir? [path]
(let [file (io/file path)]
(assert (.exists file))
(assert (.isDirectory file))
(-> file .list empty?))) ; .list ignores "." and ".."</langsyntaxhighlight>
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight lang="coffeescript">
fs = require 'fs'
 
Line 406:
fns = fs.readdirSync dir
fns.length == 0
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
Will also return <code>T</code> if <code>path</code> doesn't exist.
<langsyntaxhighlight lang="lisp">
(defun empty-directory-p (path)
(and (null (directory (concatenate 'string path "/*")))
(null (directory (concatenate 'string path "/*/")))))
</syntaxhighlight>
</lang>
 
=={{header|D}}==
 
<langsyntaxhighlight lang="d">import std.stdio, std.file;
 
void main() {
Line 429:
throw new Exception("dir not found: " ~ dirname);
return dirEntries(dirname, SpanMode.shallow).empty;
}</langsyntaxhighlight>
<pre>somedir is empty: false</pre>
=={{header|Delphi}}==
Line 435:
{{libheader| System.IOUtils}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Empty_directory;
 
Line 463:
Writeln(ParamStr(i), CHECK[IsDirectoryEmpty(ParamStr(i))], ' empty');
Readln;
end.</langsyntaxhighlight>
{{out}}
The same of c#.
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">path = hd(System.argv)
IO.puts File.dir?(path) and Enum.empty?( File.ls!(path) )</langsyntaxhighlight>
 
=={{header|Erlang}}==
Line 485:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">open System.IO
let isEmptyDirectory x = (Directory.GetFiles x).Length = 0 && (Directory.GetDirectories x).Length = 0</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USE: io.directories
: empty-directory? ( path -- ? ) directory-entries empty? ;</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
#Include "dir.bi"
Line 535:
Print
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 543:
 
=={{header|Gambas}}==
<langsyntaxhighlight lang="gambas">Public Sub Main()
Dim sFolder As String = User.home &/ "Rosetta"
Dim sDir As String[] = Dir(sFolder)
Line 554:
Print sOutput
 
End</langsyntaxhighlight>
Output:
<pre>
Line 561:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 588:
return len(entries) == 0, nil
}
</syntaxhighlight>
</lang>
 
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">def isDirEmpty = { dirName ->
def dir = new File(dirName)
dir.exists() && dir.directory && (dir.list() as List).empty
}</langsyntaxhighlight>
 
Test:
<langsyntaxhighlight lang="groovy">def currentDir = new File('.')
def random = new Random()
def subDirName = "dir${random.nextInt(100000)}"
Line 606:
 
assert ! isDirEmpty('.')
assert isDirEmpty(subDirName)</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import System.Directory (getDirectoryContents)
import System.Environment (getArgs)
 
Line 617:
f False = "Directory is not empty"
 
main = getArgs >>= isEmpty . (!! 0) >>= putStrLn</langsyntaxhighlight>
Test:
<syntaxhighlight lang="text">$ mkdir 1
$ ./isempty 1
Directory is empty
$ ./isempty /usr/
Directory is not empty</langsyntaxhighlight>
 
 
==Icon and {{header|Unicon}}==
This example uses Unicon extensions. The 'empty' sub-directory was manually setup for this test.
<langsyntaxhighlight Iconlang="icon">procedure main()
every dir := "." | "./empty" do
write(dir, if isdirempty(dir) then " is empty" else " is not empty")
Line 642:
}
else stop(s," is not a directory or will not open")
end</langsyntaxhighlight>
 
{{out}}
Line 650:
=={{header|J}}==
 
<langsyntaxhighlight lang="j">require 'dir'
empty_dir=: 0 = '/*' #@dir@,~ ]</langsyntaxhighlight>
 
In other words, list the contents of the directory, count how many items are in it, and test if that count was zero.
Line 659:
Example, under windows, create some directories using cygwin:
 
<langsyntaxhighlight lang="bash">$ mkdir /tmp/a
$ touch /tmp/a/...
$ mkdir /tmp/b
$ mkdir /tmp/c
$ mkdir /tmp/c/d</langsyntaxhighlight>
 
Then, testing these directories, in J:
 
<langsyntaxhighlight lang="j"> empty_dir 'c:/cygwin/tmp/a'
0
empty_dir 'c:/cygwin/tmp/b'
1
empty_dir 'c:/cygwin/tmp/c'
0</langsyntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|7+}}
This method does not check that the path given is actually a directory. If a path to a normal file is given, it will throw a <code>NullPointerException</code>. <code>File.listFiles()</code> does not count the "." and ".." entries.
<langsyntaxhighlight lang="java5">import java.nio.file.Paths;
//... other class code here
public static boolean isEmptyDir(String dirName){
return Paths.get(dirName).toFile().listFiles().length == 0;
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
The ECMAScript standard itself defines no IO interface – the following example makes use of the Node.js file IO library.
{{Works with|Node.js}}
<langsyntaxhighlight lang="javascript">// Node.js v14.15.4
const { readdirSync } = require("fs");
const emptydir = (path) => readdirSync(path).length == 0;
Line 694:
let dir = process.argv[i];
console.log(`${dir}: ${emptydir(dir) ? "" : "not "}empty`)
}</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia"># v0.6.0
isemptydir(dir::AbstractString) = isempty(readdir(dir))
 
@show isemptydir(".")
@show isemptydir("/home")
</syntaxhighlight>
</lang>
 
{{out}}
Line 711:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.4
 
import java.io.File
Line 719:
val isEmpty = (File(dirPath).list().isEmpty())
println("$dirPath is ${if (isEmpty) "empty" else "not empty"}")
}</langsyntaxhighlight>
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">dir('has_content') -> isEmpty
'<br />'
dir('no_content') -> isEmpty</langsyntaxhighlight>
{{out}}
<pre>false
Line 730:
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
dim info$(10, 10)
files "c:\", info$()
Line 746:
print "Folder ";folder$;" is empty."
end if
</syntaxhighlight>
</lang>
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">on isDirEmpty (dir)
return getNthFileNameInFolder(dir, 1) = EMPTY
end</langsyntaxhighlight>
 
=={{header|Lua}}==
Pure Lua function based on snipplet from Stack Overflow[http://stackoverflow.com/questions/5303174/how-to-get-list-of-directories-in-lua#11130774].
<langsyntaxhighlight lang="lua">
function scandir(directory)
local i, t, popen = 0, {}, io.popen
Line 772:
return #scandir(directory) == 0
end
</syntaxhighlight>
</lang>
 
Using lfs[https://keplerproject.github.io/luafilesystem/] library.
<langsyntaxhighlight lang="lua">
function isemptydir(directory,nospecial)
for filename in require('lfs').dir(directory) do
Line 784:
return true
end
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
 
<syntaxhighlight lang="maple">
<lang Maple>
emptydirectory := proc (dir)
is(listdir(dir) = [".", ".."]);
end proc;
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">EmptyDirectoryQ[x_] := (SetDirectory[x]; If[FileNames[] == {}, True, False])
 
Example use:
EmptyDirectoryQ["C:\\Program Files\\Wolfram Research\\Mathematica\\9"]
->True</langsyntaxhighlight>
 
=={{header|MATLAB}} / {{header|Octave}}==
<syntaxhighlight lang="matlab">
<lang Matlab>
function x = isEmptyDirectory(p)
if isdir(p)
Line 811:
end;
end;
</syntaxhighlight>
</lang>
 
=={{header|min}}==
{{works with|min|0.19.3}}
<langsyntaxhighlight lang="min">(ls bool not) :empty-dir?</langsyntaxhighlight>
 
=={{header|MS-DOS}}==
 
Since you cannot remove a directory that isn't empty, one way to check is to attempt to remove it.
<langsyntaxhighlight lang="dos">C:\>rd GAMES
Unable to remove: GAMES.
 
C:\></langsyntaxhighlight>
=={{header|Nanoquery}}==
<langsyntaxhighlight lang="nanoquery">def isempty(dirname)
return len(new(Nanoquery.IO.File).listDir(dirname)) = 0
end</langsyntaxhighlight>
 
=={{header|Nemerle}}==
<langsyntaxhighlight Nemerlelang="nemerle">using System.IO;
using System.Console;
 
Line 848:
}
}
}</langsyntaxhighlight>
 
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp">
<lang NewLISP>
(define (empty-dir? path-to-check)
(empty? (clean (lambda (x) (or (= "." x) (= ".." x))) (directory path-to-check)))
)
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import os, rdstdin
 
var empty = true
Line 864:
empty = false
break
echo empty</langsyntaxhighlight>
 
Alternatively:
 
<langsyntaxhighlight lang="nim">import os, sequtils
 
proc isEmptyDir(dir: string): bool =
Line 874:
 
echo isEmptyDir("/tmp") # false - there is always something in "/tmp"
echo isEmptyDir("/temp") # true - "/temp" does not exist</langsyntaxhighlight>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">function : IsEmptyDirectory(dir : String) ~ Bool {
return Directory->List(dir)->Size() = 0;
}</langsyntaxhighlight>
 
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">let is_dir_empty d =
Sys.readdir d = [| |]</langsyntaxhighlight>
 
=={{header|ooRexx}}==
<langsyntaxhighlight lang="oorexx">Call test 'D:\nodir' /* no such directory */
Call test 'D:\edir' /* an empty directory */
Call test 'D:\somedir' /* directory with 2 files */
Line 906:
End
End
Return</langsyntaxhighlight>
{{out}}
<pre>Directory D:\nodir not found
Line 916:
 
Define a function ''chkdir(<path>)'' that returns count of entries in a directory (without . and .. ):
<langsyntaxhighlight lang="parigp">chkdir(d)=extern(concat(["[ -d '",d,"' ]&&ls -A '",d,"'|wc -l||echo -1"]))</langsyntaxhighlight>
 
On error ''chkdir(...)'' returns -1 else count of entries. If ''chkdir() == 0'' then directory is empty. So define an additional function:
<langsyntaxhighlight lang="parigp">dir_is_empty(d)=!chkdir(d)</langsyntaxhighlight>
 
Output:<pre>chkdir("/tmp"): 52
Line 932:
However, ''GNU Pascal'' provides certain extensions to [[Extended Pascal]] (ISO 10206) so it is possible nonetheless.
Note, the following solution works only on ''strictly hierarchical'' file systems.
<langsyntaxhighlight lang="pascal">program emptyDirectory(input, output);
 
type
Line 971:
readLn(s);
writeLn(isEmptyDirectory(s))
end.</langsyntaxhighlight>
Note, symbolic links’ targets are resolved, so <tt>accessVia</tt> could in fact be a symbolic link ''to'' an empty directory.
 
=={{header|Perl}}==
===Simple version===
<langsyntaxhighlight lang="perl">sub dir_is_empty {!<$_[0]/*>}</langsyntaxhighlight>
This version however doesn't catch 'hidden' files that start with a dot.
 
Line 983:
Unfortunalety, BSD glob() doesn't handle inverted character class. If it did, this pattern could be used: <tt>{.[^.],}*</tt> (this works well in bash). But it doesn't, so there's a
===Thorough version===
<langsyntaxhighlight lang="perl">use IO::Dir;
sub dir_is_empty { !grep !/^\.{1,2}\z/, IO::Dir->new(@_)->read }</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- file i/o</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">filename</span><span style="color: #0000FF;">)</span>
Line 1,007:
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,018:
=={{header|PHP}}==
Any improvements welcome but here is a starting point for PHP
<langsyntaxhighlight lang="php">
 
$dir = 'path_here';
Line 1,037:
}
 
</syntaxhighlight>
</lang>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(prinl "myDir is" (and (dir "myDir") " not") " empty")</langsyntaxhighlight>
{{out}}
<pre>myDir is not empty</pre>
Line 1,047:
 
{{works with|PowerShell|4.0}}
<syntaxhighlight lang="powershell">
<lang PowerShell>
$path = "C:\Users"
if((Dir $path).Count -eq 0) {
Line 1,054:
"$path is not empty"
}
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 1,062:
=={{header|Prolog}}==
 
<langsyntaxhighlight Prologlang="prolog">non_empty_file('.').
non_empty_file('..').
 
empty_dir(Dir) :-
directory_files(Dir, Files),
maplist(non_empty_file, Files).</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang="purebasic">Procedure isDirEmpty(path$)
If Right(path$, 1) <> "\": path$ + "\": EndIf
Protected dirID = ExamineDirectory(#PB_Any, path$, "*.*")
Line 1,098:
EndIf
MessageRequester("Empty directory test", #DQUOTE$ + path$ + #DQUOTE$ + result$)
EndIf </langsyntaxhighlight>
{{out}} when selecting directories "L:\vv\6\" and "L:\vv\" :
<pre>"L:\vv\6\" is empty.
Line 1,106:
=={{header|Python}}==
{{works with|Python|2.x}}
<langsyntaxhighlight lang="python">import os;
if os.listdir(raw_input("directory")):
print "not empty"
else:
print "empty"
</syntaxhighlight>
</lang>
 
=={{header|R}}==
<syntaxhighlight lang="r">
<lang R>
is_dir_empty <- function(path){
if(length(list.files(path)) == 0)
Line 1,121:
is_dir_empty(path)
</langsyntaxhighlight>
 
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">
#lang racket
(empty? (directory-list "some-directory"))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>sub dir-is-empty ($d) { not dir $d }</langsyntaxhighlight>
The <tt>dir</tt> function returns a lazy list of filenames, excluding "<tt>.</tt>" and "<tt>..</tt>" automatically. Any boolean context (in this case the <tt>not</tt> function) will do just enough work on the lazy list to determine whether there are any elements, so we don't have to count the directory entries, or even read them all into memory, if there are more than one buffer's worth.
 
Line 1,138:
{{works with|Regina}}
The following program was tested in a DOS window under Windows/XP and should work for all Microsoft Windows.
<langsyntaxhighlight lang="rexx">/*REXX pgm checks to see if a directory is empty; if not, lists entries.*/
parse arg xdir; if xdir='' then xdir='\someDir' /*Any DIR? Use default.*/
@.=0 /*default in case ADDRESS fails. */
Line 1,151:
if #==0 then #=' no ' /*use a word, ¬zero.*/
say center('directory ' xdir " has " # ' entries.',79,'─')
exit @.0+rc /*stick a fork in it, we're done.*/</langsyntaxhighlight>
{{out}} when the following input was used: &nbsp; <tt> temp </tt>
<pre>
Line 1,162:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
myList = dir("C:\Ring\bin")
if len(myList) > 0 see "C:\Ring\bin is not empty" + nl
else see "C:\Ring\bin is empty" + nl ok
</syntaxhighlight>
</lang>
 
=={{header|Ruby}}==
Raises a SystemCallError if the named directory doesn’t exist.
<langsyntaxhighlight lang="ruby">Dir.entries("testdir").empty? </langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">files #f, DefaultDir$ + "\*.*" ' open some directory.
print "hasanswer: ";#f HASANSWER() ' if it has an answer it is not MT
print "rowcount: ";#f ROWCOUNT() ' if not MT, how many files?</langsyntaxhighlight>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">use std::fs::read_dir;
use std::error::Error;
 
Line 1,198:
}
}
}</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">import java.io.File
 
def isDirEmpty(file:File) : Boolean =
return file.exists && file.isDirectory && file.list.isEmpty</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "osfiles.s7i";
 
Line 1,216:
begin
writeln(dirEmpty("somedir"));
end func;</langsyntaxhighlight>
 
=={{header|SenseTalk}}==
The filesAndFolders function returns an empty list if a directory is empty (and does not return '.' or '..').
<langsyntaxhighlight lang="sensetalk">put the temporary folder & "NewFolder" into newFolderPath
make folder newFolderPath -- create a new empty directory
 
Line 1,228:
put "Something is present in " & newFolderPath
end if
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,236:
=={{header|Sidef}}==
Built-in method:
<langsyntaxhighlight lang="ruby">Dir.new('/my/dir').is_empty; # true, false or nil</langsyntaxhighlight>
 
User-defined function:
<langsyntaxhighlight lang="ruby">func is_empty(dir) {
dir.open(\var dir_h) || return nil;
dir_h.each { |file|
Line 1,246:
};
return true;
};</langsyntaxhighlight>
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang="sml">fun isDirEmpty(path: string) =
let
val dir = OS.FileSys.openDir path
Line 1,260:
| _ => false
)
end;</langsyntaxhighlight>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">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 {^\.\.?$}]]}
}</langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
<langsyntaxhighlight lang="bash">
#!/bin/sh
DIR=/tmp/foo
[ `ls -a $DIR|wc -l` -gt 2 ] && echo $DIR is NOT empty || echo $DIR is empty
</syntaxhighlight>
</lang>
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Sub Main()
Debug.Print IsEmptyDirectory("C:\Temp")
Debug.Print IsEmptyDirectory("C:\Temp\")
Line 1,288:
D = IIf(Right(D, 1) <> Sep, D & Sep, D)
IsEmptyDirectory = (Dir(D & "*.*") = "")
End Function</langsyntaxhighlight>
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
Function IsDirEmpty(path)
IsDirEmpty = False
Line 1,304:
WScript.StdOut.WriteLine IsDirEmpty("C:\Temp")
WScript.StdOut.WriteLine IsDirEmpty("C:\Temp\test")
</syntaxhighlight>
</lang>
 
{{Out}}
Line 1,313:
 
=={{header|Vlang}}==
<langsyntaxhighlight lang="vlang">import os
 
fn main() {
Line 1,324:
return 'Directory not empty!'
}
</syntaxhighlight>
</lang>
 
=={{header|Wren}}==
<langsyntaxhighlight lang="ecmascript">import "io" for Directory
 
var isEmptyDir = Fn.new { |path|
Line 1,336:
var path = "test"
var empty = isEmptyDir.call(path)
System.print("'%(path)' is %(empty ? "empty" : "not empty")")</langsyntaxhighlight>
 
{{out}}
Line 1,345:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">path:="Empty"; File.isDir(path).println();
File.mkdir(path); File.isDir(path).println();
File.glob(path+"/*").println(); // show contents of directory</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits