Empty directory: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
MaiconSoft (talk | contribs) (Added Delphi example) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(29 intermediate revisions by 14 users not shown) | |||
Line 6:
=={{header|11l}}==
<
print(‘empty’)
E
print(‘not empty’)</
=={{header|8086 Assembly}}==
{{trans|MS-DOS}}
<syntaxhighlight 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.
mov ds,ax ;load the segment register DS with the segment of dirname
mov dx,offset dirname ;load into DX the offset of dirname
mov ah,39h ;0x39 is the interrupt code for remove directory
int 21h ;sets carry if error is encountered. error code will be in AX.
;If carry is clear the remove was successful
jc error
mov ah,4Ch ;return function
mov al,0 ;required return code
int 21h ;return to DOS
error: ;put your error handler here
mov ah,4Ch ;return function
mov al,0 ;required return code
int 21h ;return to DOS
dirname db "GAMES",0</syntaxhighlight>
=={{header|Ada}}==
<
with Ada.Directories;
procedure EmptyDir is
Line 35 ⟶ 62:
Put_Line (Empty ("./emptydir.adb"));
Put_Line (Empty ("./foobar"));
end EmptyDir;</
{{out}}
<pre>Not empty
Line 46 ⟶ 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 "-".
<
PROC is empty directory = ( STRING directory )BOOL:
IF NOT file is directory( directory )
Line 77 ⟶ 104:
print( ( argv( i ), " is ", IF is empty directory( argv( i ) ) THEN "empty" ELSE "not empty" FI, newline ) )
FI
OD</
{{out}}
<pre>
Line 85 ⟶ 112:
\zz is empty
</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">emptyDir?: function [folder]-> empty? list folder
print emptyDir? "."</syntaxhighlight>
{{out}}
<pre>false</pre>
=={{header|AutoHotkey}}==
<
isDir_empty(p) {
Line 93 ⟶ 130:
return 0
return 1
}</
{{out}}
<pre>false</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f EMPTY_DIRECTORY.AWK
BEGIN {
Line 136 ⟶ 173:
return(msg)
}
</syntaxhighlight>
{{out}}
<pre>
Line 145 ⟶ 182:
</pre>
=={{header|
==={{header|BaCon}}===
<syntaxhighlight lang="basic">FUNCTION check$(dir$)
IF FILEEXISTS(dir$) THEN
Line 163 ⟶ 201:
dir$ = "."
PRINT "Directory '", dir$, "'", check$(dir$)</
{{out}}
Line 176 ⟶ 214:
*2 - input directory does not exist.
*3 - input not found.
<
if "%~1"=="" exit /b 3
set "samp_path=%~1"
Line 205 ⟶ 243:
:folder_not_found
echo Folder not found.
exit /b 2</
{{Out|Sample Session}}
(Saved the Batch File as IsEmpty.Bat in C:\)
Line 237 ⟶ 275:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
IF FNisdirectoryempty("C:\temp") PRINT "C:\temp is empty" ELSE PRINT "C:\temp is not empty"
END
Line 253 ⟶ 291:
UNTIL res% == 0
SYS "FindClose", sh%
= (res% == 0)</
=={{header|C}}==
<
#include <dirent.h>
#include <string.h>
Line 295 ⟶ 333:
return 0;
}</
% mkdir stuff; ./a.out /usr/ ./stuff /etc/passwd
/usr/: not empty
Line 302 ⟶ 340:
</pre>
=={{header|C sharp|C#}}==
<
using System.IO;
Line 322 ⟶ 360:
}
}
</syntaxhighlight>
Running it:<pre>
Assume c:\temp exists and is not empty, c:\temp\empty exists and is empty
Line 333 ⟶ 371:
=={{header|C++}}==
{{libheader|Boost}}
<
#include <iostream>
#include <boost/filesystem.hpp>
Line 350 ⟶ 388:
}
}
</syntaxhighlight>
=={{header|Clojure}}==
<
(defn empty-dir? [path]
(let [file (io/file path)]
(assert (.exists file))
(assert (.isDirectory file))
(-> file .list empty?))) ; .list ignores "." and ".."</
=={{header|CoffeeScript}}==
<
fs = require 'fs'
Line 369 ⟶ 407:
fns = fs.readdirSync dir
fns.length == 0
</syntaxhighlight>
=={{header|Common Lisp}}==
Will also return <code>T</code> if <code>path</code> doesn't exist.
<
(defun empty-directory-p (path)
(and (null (directory (concatenate 'string path "/*")))
(null (directory (concatenate 'string path "/*/")))))
</syntaxhighlight>
=={{header|D}}==
<
void main() {
Line 392 ⟶ 430:
throw new Exception("dir not found: " ~ dirname);
return dirEntries(dirname, SpanMode.shallow).empty;
}</
<pre>somedir is empty: false</pre>
=={{header|Delphi}}==
Line 398 ⟶ 436:
{{libheader| System.IOUtils}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
program Empty_directory;
Line 426 ⟶ 464:
Writeln(ParamStr(i), CHECK[IsDirectoryEmpty(ParamStr(i))], ' empty');
Readln;
end.</
{{out}}
The same of c#.
=={{header|Elixir}}==
<
IO.puts File.dir?(path) and Enum.empty?( File.ls!(path) )</
=={{header|Erlang}}==
Line 448 ⟶ 486:
=={{header|F_Sharp|F#}}==
<
let isEmptyDirectory x = (Directory.GetFiles x).Length = 0 && (Directory.GetDirectories x).Length = 0</
=={{header|Factor}}==
<
: empty-directory? ( path -- ? ) directory-entries empty? ;</
=={{header|FreeBASIC}}==
<
#Include "dir.bi"
Line 498 ⟶ 536:
Print
Print "Press any key to quit"
Sleep</
{{out}}
<pre>
'c:\freebasic\docs' is empty
</pre>
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
local fn DirectoryContents( url as CFURLRef ) as CFArrayRef
CFArrayRef contents = fn FileManagerContentsOfDirectoryAtURL( url, NULL, NSDirectoryEnumerationSkipsHiddenFiles )
if ( contents == NULL )
NSLog(@"Unable to get contents of directory \"%@\".",fn URLLastPathComponent(url))
end if
end fn = fn ArrayValueForKey( contents, @"lastPathComponent" )
void local fn DoIt
CFURLRef dirURL, fileURL
CFArrayRef contents
dirURL = fn URLFileURLWithPath( fn StringByExpandingTildeInPath( @"~/Desktop/docs" ) )
if ( fn FileManagerCreateDirectoryAtURL( dirURL, YES, NULL ) )
contents = fn DirectoryContents( dirURL )
if ( contents )
NSLog(@"Directory \"docs\" \b")
if ( len(contents) )
NSLog(@"contents:\n%@",contents)
else
NSLog(@"is empty.")
end if
NSLog(@"")
fileURL = fn URLFileURLWithPath( fn StringByExpandingTildeInPath( @"~/Desktop/docs/output.txt" ) )
if (fn FileManagerCreateFileAtURL( fileURL, NULL, NULL ) )
contents = fn DirectoryContents( dirURL )
NSLog(@"Directory \"docs\" \b")
if ( len(contents) )
NSLog(@"contents:\n%@",contents)
else
NSLog(@"is empty.")
end if
end if
end if
end if
end fn
fn DoIt
HandleEvents
</syntaxhighlight>
{{out}}
<pre>
Directory "docs" is empty.
Directory "docs" contents:
(
"output.txt"
)
</pre>
=={{header|Gambas}}==
<
Dim sFolder As String = User.home &/ "Rosetta"
Dim sDir As String[] = Dir(sFolder)
Line 517 ⟶ 614:
Print sOutput
End</
Output:
<pre>
Line 524 ⟶ 621:
=={{header|Go}}==
<
import (
Line 551 ⟶ 648:
return len(entries) == 0, nil
}
</syntaxhighlight>
=={{header|Groovy}}==
Solution:
<
def dir = new File(dirName)
dir.exists() && dir.directory && (dir.list() as List).empty
}</
Test:
<
def random = new Random()
def subDirName = "dir${random.nextInt(100000)}"
Line 569 ⟶ 666:
assert ! isDirEmpty('.')
assert isDirEmpty(subDirName)</
=={{header|Haskell}}==
<
import System.Environment (getArgs)
Line 580 ⟶ 677:
f False = "Directory is not empty"
main = getArgs >>= isEmpty . (!! 0) >>= putStrLn</
Test:
<syntaxhighlight lang="text">$ mkdir 1
$ ./isempty 1
Directory is empty
$ ./isempty /usr/
Directory is not empty</
==Icon and {{header|Unicon}}==
This example uses Unicon extensions. The 'empty' sub-directory was manually setup for this test.
<
every dir := "." | "./empty" do
write(dir, if isdirempty(dir) then " is empty" else " is not empty")
Line 605 ⟶ 702:
}
else stop(s," is not a directory or will not open")
end</
{{out}}
Line 613 ⟶ 710:
=={{header|J}}==
<
empty_dir=: 0 = '/*' #@dir@,~ ]</
In other words, list the contents of the directory, count how many items are in it, and test if that count was zero.
Line 622 ⟶ 719:
Example, under windows, create some directories using cygwin:
<
$ touch /tmp/a/...
$ mkdir /tmp/b
$ mkdir /tmp/c
$ mkdir /tmp/c/d</
Then, testing these directories, in J:
<
0
empty_dir 'c:/cygwin/tmp/b'
1
empty_dir 'c:/cygwin/tmp/c'
0</
=={{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.
<
//... other class code here
public static boolean isEmptyDir(String dirName){
return Paths.get(dirName).toFile().listFiles().length == 0;
}</
=={{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}}
<syntaxhighlight lang="javascript">// Node.js v14.15.4
const { readdirSync } = require("fs");
const emptydir = (path) => readdirSync(path).length == 0;
// tests, run like node emptydir.js [directories]
for (let i = 2; i < process.argv.length; i ++) {
let dir = process.argv[i];
console.log(`${dir}: ${emptydir(dir) ? "" : "not "}empty`)
}</syntaxhighlight>
=={{header|Julia}}==
<
isemptydir(dir::AbstractString) = isempty(readdir(dir))
@show isemptydir(".")
@show isemptydir("/home")
</syntaxhighlight>
{{out}}
Line 661 ⟶ 771:
=={{header|Kotlin}}==
<
import java.io.File
Line 669 ⟶ 779:
val isEmpty = (File(dirPath).list().isEmpty())
println("$dirPath is ${if (isEmpty) "empty" else "not empty"}")
}</
=={{header|Lasso}}==
<
'<br />'
dir('no_content') -> isEmpty</
{{out}}
<pre>false
Line 680 ⟶ 790:
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
dim info$(10, 10)
files "c:\", info$()
Line 696 ⟶ 806:
print "Folder ";folder$;" is empty."
end if
</syntaxhighlight>
=={{header|Lingo}}==
<
return getNthFileNameInFolder(dir, 1) = EMPTY
end</
=={{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].
<
function scandir(directory)
local i, t, popen = 0, {}, io.popen
Line 722 ⟶ 832:
return #scandir(directory) == 0
end
</syntaxhighlight>
Using lfs[https://keplerproject.github.io/luafilesystem/] library.
<
function isemptydir(directory,nospecial)
for filename in require('lfs').dir(directory) do
Line 734 ⟶ 844:
return true
end
</syntaxhighlight>
=={{header|Maple}}==
<syntaxhighlight lang="maple">
emptydirectory := proc (dir)
is(listdir(dir) = [".", ".."]);
end proc;
</syntaxhighlight>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
Example use:
EmptyDirectoryQ["C:\\Program Files\\Wolfram Research\\Mathematica\\9"]
->True</
=={{header|MATLAB}} / {{header|Octave}}==
<syntaxhighlight lang="matlab">
function x = isEmptyDirectory(p)
if isdir(p)
Line 761 ⟶ 871:
end;
end;
</syntaxhighlight>
=={{header|min}}==
{{works with|min|0.19.3}}
<
=={{header|MS-DOS}}==
Since you cannot remove a directory that isn't empty, one way to check is to attempt to remove it.
<syntaxhighlight lang="dos">C:\>rd GAMES
Unable to remove: GAMES.
C:\></syntaxhighlight>
=={{header|Nanoquery}}==
<
return len(new(Nanoquery.IO.File).listDir(dirname)) = 0
end</
=={{header|Nemerle}}==
<
using System.Console;
Line 791 ⟶ 908:
}
}
}</
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp">
(define (empty-dir? path-to-check)
(empty? (clean (lambda (x) (or (= "." x) (= ".." x))) (directory path-to-check)))
)
</syntaxhighlight>
=={{header|Nim}}==
<
var empty = true
Line 807 ⟶ 924:
empty = false
break
echo empty</
Alternatively:
<
proc isEmptyDir(dir: string): bool =
Line 817 ⟶ 934:
echo isEmptyDir("/tmp") # false - there is always something in "/tmp"
echo isEmptyDir("/temp") # true - "/temp" does not exist</
=={{header|Objeck}}==
<
return Directory->List(dir)->Size() = 0;
}</
=={{header|OCaml}}==
<
Sys.readdir d = [| |]</
=={{header|ooRexx}}==
<
Call test 'D:\edir' /* an empty directory */
Call test 'D:\somedir' /* directory with 2 files */
Line 849 ⟶ 966:
End
End
Return</
{{out}}
<pre>Directory D:\nodir not found
Line 859 ⟶ 976:
Define a function ''chkdir(<path>)'' that returns count of entries in a directory (without . and .. ):
<
On error ''chkdir(...)'' returns -1 else count of entries. If ''chkdir() == 0'' then directory is empty. So define an additional function:
<
Output:<pre>chkdir("/tmp"): 52
dir_is_empty("/tmp"): 0</pre>
=={{header|Pascal}}==
{{works with|GNU Pascal}}
Pascal does not know the notion of directories.
Therefore it is ''not possible'' to determine whether a directory is empty.
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.
<syntaxhighlight lang="pascal">program emptyDirectory(input, output);
type
path = string(1024);
{
\brief determines whether a (hierarchial FS) directory is empty
\param accessVia a possible route to access a directory
\return whether \param accessVia is an empty directory
}
function isEmptyDirectory(protected accessVia: path): Boolean;
var
{ NB: `file` data types without a domain type are non-standard }
directory: bindable file;
FD: bindingType;
begin
{ initialize variables }
unbind(directory);
FD := binding(directory);
FD.name := accessVia;
{ binding to directories is usually not possible }
FD.force := true;
{ the actual test }
bind(directory, FD);
FD := binding(directory);
unbind(directory);
isEmptyDirectory := FD.bound and FD.directory and (FD.links <= 2)
end;
{ === MAIN ============================================================= }
var
s: path;
begin
readLn(s);
writeLn(isEmptyDirectory(s))
end.</syntaxhighlight>
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===
<
This version however doesn't catch 'hidden' files that start with a dot.
Line 877 ⟶ 1,043:
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===
<
sub dir_is_empty { !grep !/^\.{1,2}\z/, IO::Dir->new(@_)->read }</
=={{header|Phix}}==
<!--<syntaxhighlight lang="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>
<span style="color: #004080;">string</span> <span style="color: #000000;">msg</span>
<span style="color: #008080;">switch</span> <span style="color: #7060A8;">get_file_type</span><span style="color: #0000FF;">(</span><span style="color: #000000;">filename</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">case</span> <span style="color: #000000;">FILETYPE_UNDEFINED</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">msg</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"is UNDEFINED"</span>
<span style="color: #008080;">case</span> <span style="color: #000000;">FILETYPE_NOT_FOUND</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">msg</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"is NOT_FOUND"</span>
<span style="color: #008080;">case</span> <span style="color: #004600;">FILETYPE_FILE</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">msg</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"is a FILE"</span>
<span style="color: #008080;">case</span> <span style="color: #004600;">FILETYPE_DIRECTORY</span><span style="color: #0000FF;">:</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">vslice</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">dir</span><span style="color: #0000FF;">(</span><span style="color: #000000;">filename</span><span style="color: #0000FF;">),</span><span style="color: #004600;">D_NAME</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"out"</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"."</span><span style="color: #0000FF;">,</span><span style="color: #008000;">".."</span><span style="color: #0000FF;">}))</span>
<span style="color: #000000;">msg</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">count</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"is an empty directory"</span>
<span style="color: #0000FF;">:</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"is a directory containing %d files"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">}))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">switch</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">filename</span><span style="color: #0000FF;">,</span><span style="color: #000000;">msg</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"C:\\xx"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"C:\\not_there"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"C:\\Program Files (x86)\\Phix\\p.exe"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"C:\\Windows"</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 918 ⟶ 1,078:
=={{header|PHP}}==
Any improvements welcome but here is a starting point for PHP
<
$dir = 'path_here';
Line 937 ⟶ 1,097:
}
</syntaxhighlight>
=={{header|PicoLisp}}==
<
{{out}}
<pre>myDir is not empty</pre>
Line 947 ⟶ 1,107:
{{works with|PowerShell|4.0}}
<syntaxhighlight lang="powershell">
$path = "C:\Users"
if((Dir $path).Count -eq 0) {
Line 954 ⟶ 1,114:
"$path is not empty"
}
</syntaxhighlight>
<b>Output:</b>
<pre>
Line 962 ⟶ 1,122:
=={{header|Prolog}}==
<
non_empty_file('..').
empty_dir(Dir) :-
directory_files(Dir, Files),
maplist(non_empty_file, Files).</
=={{header|PureBasic}}==
<
If Right(path$, 1) <> "\": path$ + "\": EndIf
Protected dirID = ExamineDirectory(#PB_Any, path$, "*.*")
Line 998 ⟶ 1,158:
EndIf
MessageRequester("Empty directory test", #DQUOTE$ + path$ + #DQUOTE$ + result$)
EndIf </
{{out}} when selecting directories "L:\vv\6\" and "L:\vv\" :
<pre>"L:\vv\6\" is empty.
Line 1,006 ⟶ 1,166:
=={{header|Python}}==
{{works with|Python|2.x}}
<
if os.listdir(raw_input("directory")):
print "not empty"
else:
print "empty"
</syntaxhighlight>
=={{header|R}}==
<syntaxhighlight lang="r">
is_dir_empty <- function(path){
if(length(list.files(path)) == 0)
print("This folder is empty")
}
is_dir_empty(path)
</syntaxhighlight>
=={{header|Racket}}==
<
#lang racket
(empty? (directory-list "some-directory"))
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
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,028 ⟶ 1,198:
{{works with|Regina}}
The following program was tested in a DOS window under Windows/XP and should work for all Microsoft Windows.
<
parse arg xdir; if xdir='' then xdir='\someDir' /*Any DIR? Use default.*/
@.=0 /*default in case ADDRESS fails. */
Line 1,041 ⟶ 1,211:
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.*/</
{{out}} when the following input was used: <tt> temp </tt>
<pre>
Line 1,052 ⟶ 1,222:
=={{header|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>
=={{header|RPL}}==
≪ 1 OVER SIZE '''FOR''' j
DUP j GET EVAL
'''NEXT''' DROP
VARS SIZE NOT
≫ ''''MTDIR'''' STO
{{in}}
<pre>
{ HOME °RZTA °TEST } MTDIR
</pre>
{{out}}
<pre>
1: 0
</pre>
=={{header|Ruby}}==
Raises a SystemCallError if the named directory doesn’t exist.
<
=={{header|Run BASIC}}==
<
print "hasanswer: ";#f HASANSWER() ' if it has an answer it is not MT
print "rowcount: ";#f ROWCOUNT() ' if not MT, how many files?</
=={{header|Rust}}==
<
use std::error::Error;
Line 1,088 ⟶ 1,273:
}
}
}</
=={{header|Scala}}==
<
def isDirEmpty(file:File) : Boolean =
return file.exists && file.isDirectory && file.list.isEmpty</
=={{header|Seed7}}==
<
include "osfiles.s7i";
Line 1,106 ⟶ 1,291:
begin
writeln(dirEmpty("somedir"));
end func;</
=={{header|SenseTalk}}==
The filesAndFolders function returns an empty list if a directory is empty (and does not return '.' or '..').
<syntaxhighlight lang="sensetalk">put the temporary folder & "NewFolder" into newFolderPath
make folder newFolderPath -- create a new empty directory
if the filesAndFolders in newFolderPath is empty then
put "Directory " & newFolderPath & " is empty!"
else
put "Something is present in " & newFolderPath
end if
</syntaxhighlight>
{{out}}
<pre>
Directory /var/folders/dp/0nmm3z4m8xl5m3008k8db78h0000gn/T/NewFolder is empty!
</pre>
=={{header|Sidef}}==
Built-in method:
<
User-defined function:
<
dir.open(\var dir_h) || return nil;
dir_h.each { |file|
Line 1,120 ⟶ 1,321:
};
return true;
};</
=={{header|Standard ML}}==
<
let
val dir = OS.FileSys.openDir path
Line 1,134 ⟶ 1,335:
| _ => false
)
end;</
=={{header|Tcl}}==
<
# 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 {^\.\.?$}]]}
}</
=={{header|UNIX Shell}}==
<
#!/bin/sh
DIR=/tmp/foo
[ `ls -a $DIR|wc -l` -gt 2 ] && echo $DIR is NOT empty || echo $DIR is empty
</syntaxhighlight>
=={{header|VBA}}==
<
Debug.Print IsEmptyDirectory("C:\Temp")
Debug.Print IsEmptyDirectory("C:\Temp\")
Line 1,162 ⟶ 1,363:
D = IIf(Right(D, 1) <> Sep, D & Sep, D)
IsEmptyDirectory = (Dir(D & "*.*") = "")
End Function</
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
Function IsDirEmpty(path)
IsDirEmpty = False
Line 1,178 ⟶ 1,379:
WScript.StdOut.WriteLine IsDirEmpty("C:\Temp")
WScript.StdOut.WriteLine IsDirEmpty("C:\Temp\test")
</syntaxhighlight>
{{Out}}
Line 1,185 ⟶ 1,386:
True
</pre>
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import os
fn main() {
println(is_empty_dir('../Check'))
}
fn is_empty_dir(name string) string {
if os.is_dir(name) == false {return 'Directory name not exist!'}
if os.is_dir(name) && os.is_dir_empty(name) == true {return 'Directory exists and is empty!'}
return 'Directory not empty!'
}
</syntaxhighlight>
=={{header|Wren}}==
<
var isEmptyDir = Fn.new { |path|
Line 1,196 ⟶ 1,411:
var path = "test"
var empty = isEmptyDir.call(path)
System.print("'%(path)' is %(empty ? "empty" : "not empty")")</
{{out}}
Line 1,205 ⟶ 1,420:
=={{header|zkl}}==
<
File.mkdir(path); File.isDir(path).println();
File.glob(path+"/*").println(); // show contents of directory</
{{out}}
<pre>
|