List empty directories


/ Published in: Perl
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!/usr/bin/perl -w
  2. use strict;
  3. #
  4. # List empty directories
  5. # perl administration posix
  6. #
  7.  
  8.  
  9. # set start path
  10.  
  11. my $startpath = shift || '.';
  12.  
  13.  
  14. sub checkpath {
  15. my $path = shift;
  16. # open path or die
  17. opendir my($dir), $path
  18. or die "Can't open $path : $!\n";
  19. # debug
  20. #print "I am here: $path\n";
  21. # get directory content but skip . and .. (to avoid circular looping)
  22. my @content = grep {$_ !~ /^\.\.?$/} readdir $dir;
  23. # print directory name and exit if empty
  24. if (!@content) {
  25. print "$path\n";
  26. }
  27. # recurse trough directories
  28. foreach my $subpath (grep { -d "$path/$_"} @content) {
  29. checkpath($path.'/'.$subpath);
  30. }
  31. }
  32.  
  33. checkpath($startpath);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.