Re: Check if directory is empty on Win32



On Jun 26, 5:50 pm, paru...@xxxxxxxxx (Leonid L) wrote:
Many of the proposed solutions I've found on Google do not work for
me, perhaps because they assume Unix/Linux host.

Or, perhaps because you're doing something wrong? How about posting
one of these methods that "don't work", so we can evaluate for
ourselves?

I need a sub that
will reliably tell me whether a given directory is empty (I am running
Perl on Win XP, NTFS file system). Please give your implementation a
quick test on a similar platform before posting.

There's a couple hundred ways. Here's two:

#!/usr/bin/perl
use strict;
use warnings;

mkdir("working_dir") or die "Cannot create working_dir: $!";
chdir("working_dir") or die "Cannot change to working_dir: $!";

mkdir("empty") or die "Can't create empty: $!";
mkdir("subdir") or die "Can't create subdir: $!";
mkdir("subdir/foo") or die "Can't create subdir/foo: $!";
mkdir("file") or die "Can't create file: $!";
open my $ofh, '>', "file/bar" or die "Can't create file/bar: $!";
close $ofh;
mkdir("dotfile") or die "Can't create dotfile: $!";
open my $ofh2, '>', "dotfile/.baz" or die "Can't create dotfile/.baz:
$!"; close $ofh2;

for my $dir (qw/empty subdir file dotfile/) {
print is_empty1($dir) ? "$dir is empty\n" : "$dir is not empty\n";
print is_empty2($dir) ? "$dir is empty\n" : "$dir is not empty\n";
}

sub is_empty1 {
my $dir = shift;
my @contents = grep { ! /^$dir\/\.\.?$/ } glob("$dir/* $dir/.*");
return @contents == 0;
}

sub is_empty2 {
my $dir = shift;
opendir my $dh, $dir or die "Cannot open $dir: $!";
my @contents = grep { ! /^\.\.?$/ } readdir($dh);
return @contents == 0;
}
__END__

Results:
empty is empty
empty is empty
subdir is not empty
subdir is not empty
file is not empty
file is not empty
dotfile is not empty
dotfile is not empty


This is perl, v5.10.0 built for MSWin32-x86-multi-thread


Paul Lalli

.



Relevant Pages

  • Re: The ufs log is empty and will not be used
    ... Due diligence was done before posting. ... It means the ufs log is empty: / no point in using it. ... where the previous shutdown or power down wasnt exactly "intended".. ...
    (comp.unix.solaris)
  • Re: Recycle Bin error : "do you really want to delet WINDOWS" ?
    ... applied Kelly's tip already before posting as well: ... >>it and select empty recycle bin, I get the above mentionned message about ... just shutdown and restart Windows to see if that fixed it. ... you could have a corrupted recycle bin. ...
    (microsoft.public.windowsxp.general)
  • Re: Error With Matlab GUI
    ... popupmenu control requires a non-empty String ... in are empty strings, empty arrays, or empty cell arrays. ... see the posting I made just a moment ago to someone else in which I showed a routine ...
    (comp.soft-sys.matlab)
  • Re: how do I identify statistical outliers with excel?
    ... For the newsreader-impaired, the MS web interface is not the best newsreader ... "do not permit posting responses when the body of the posting is ... limited to "empty" postings.) ... might not see responses to their "empty" postings. ...
    (microsoft.public.excel.misc)
  • Re: How is possible that avg_data_blocks_per_key = 0 even with fresh statistics ?
    ... next week how are their index statistics. ... actually empty (i checked only some. ... Many people won't respond from cdos if you continue top posting. ... As best I understand it almost any plan the optimizer ...
    (comp.databases.oracle.server)