in reply to Re: delete dir if empty on nt
in thread delete dir if empty on nt

appologies for the lack of clarity. I am working on tidying up disk space based on age of files using the -M switch. If the file is over -M days the file is deleted. The program I have which works sucesfully traverses a tree and deletes the files that meet the test condition. Naturaly this then leaves me with a set of dirs which may or may not be empty. Therefore the net thing to do is find and delete any empty directories. At this stage I dont care about their age. Hope this is clearer

Replies are listed 'Best First'.
Re^3: delete dir if empty on nt
by Aristotle (Chancellor) on Nov 18, 2002 at 22:39 UTC
    I assume you're doing the right thing and using File::Find to find the files in question - then the job can be done at the drop of a hat. The postprocess predicate to find() calls a given sub just before finally exiting a directory. That's all we need - we simply attempt to remove the directory and if there's anything in there, that will just fail harmlessly. It's less work for both us and the computer than if we tried to check manually.
    #!/usr/bin/perl -w use strict; use File::Find; my $max_age_days = 120; find({ wanted => sub { unlink if -f $_ and -M _ > $max_age_days }, postprocess => sub { rmdir $File::Find::dir }, }, @ARGV);

    Makeshifts last the longest.