I am converting a linux script from http://www.perlmonks.org/index.pl?node_id=217166 specifically this:
#!/usr/bin/perl -w use strict; use Getopt::Std; use File::Find; @ARGV > 0 and getopts('a:', \my %opt) or die << 'USAGE'; # Deletes any old files from the directory tree(s) given and # removes empty directories en passant. usage: $0 [-a maxage] directory [directory ...] -a maximum age in days, default is 120 USAGE my $max_age_days = $opt{a} || 120; find({ wanted => sub { unlink if -f $_ and -M _ > $max_age_days }, postprocess => sub { rmdir $File::Find::dir }, }, @ARGV);
my attempt is:
#!/usr/bin/perl -w use strict; use Getopt::Std; use File::Find; @ARGV > 0 and getopts('a:', \my %opt) or die << 'USAGE'; # Deletes any old files from the directory tree(s) given and # removes empty directories en passant. usage: $0 [-a maxage] directory [directory ...] -a maximum age in days, default is 120 USAGE my $max_age_days = $opt{a} || 120; find({ wanted => sub { unlink if -f $_ and -M _ > $max_age_days }, # postprocess => sub { rmdir $File::Find::dir }, postprocess => sub { my $expr = '$File::Find::dir'; $expr =~ s/\//\\/g; # replace / with \ print 'rmdir $expr\n'; `rmdir $expr`; }, }, @ARGV);
However I get an error when the script tries to remove a directory saying that the directory is in use by another process (when it isn’t). Any ideas? I’m running the script on Windows Server 2003 SP2 64-bit using ActiveState 5.10.
Thanks!
From this documentation
This means that your own code is still using the directory as you try to delete it. Try building a list of names and iterating through that after the call to find.
Another possible solution is to use the
no_chdiroption to avoid having find use the directories you want to delete.EDIT: This comment is also relevant, so i’m promoting it to the main answer’s body: