Empty directory: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Java}}: I added this from my phone and somehow it autocorrected the method name...)
Line 49: Line 49:
/etc/passwd: Not a directory
/etc/passwd: Not a directory
</pre>
</pre>

==Icon and {{header|Unicon}}==
This example uses Unicon extensions. The 'empty' sub-directory was manually setup for this test.
<lang Icon>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</lang>

Output:<pre>. is not empty
./empty is empty</pre>


=={{header|Java}}==
=={{header|Java}}==

Revision as of 02:49, 5 January 2012

Empty directory is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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 “.” and almost every directory contains “..” (except for a root directory); an empty directory contains no other entries.

C

<lang c>#include <stdio.h>

  1. include <dirent.h>
  2. include <string.h>

int dir_empty(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;

}</lang>Running it:

% mkdir stuff; ./a.out /usr/ ./stuff /etc/passwd
/usr/: not empty
./stuff: empty
/etc/passwd: Not a directory

Icon and Unicon

This example uses Unicon extensions. The 'empty' sub-directory was manually setup for this test. <lang Icon>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</lang>

Output:

. is not empty
./empty is empty

Java

Works with: Java version 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 NullPointerException. File.listFiles() does not count the "." and ".." entries. <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;

}</lang>

Ruby

Works with: Ruby version 1.8.7

<lang ruby># Checks if a directory is empty, but raises SystemCallError

  1. if _path_ is not a directory.

def empty_dir?(path)

 not Dir.foreach(path).detect {|f| f != '.' and f != '..'}

end</lang>

If Ruby is older than 1.8.7, then Dir.foreach must take a block.

<lang ruby># Checks if a directory is empty, but raises SystemCallError

  1. if _path_ is not a directory.

def empty_dir?(path)

 Dir.foreach(path) {|f|
   return false if f != '.' and f != '..'
 }
 return true

end</lang>

Tcl

<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 {^\.\.?$}]]}

}</lang>