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
postprocess
The value should be a code reference. It is invoked just before leaving the currently processed directory. It is called in void context with no arguments. The name of the current directory is in $File::Find::dir. This hook is handy for summarizing a directory, such as calculating its disk usage. When follow or follow_fast are in effect, postprocess is a no-op.
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_chdir
option 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:
To add to that: the issue here is that on Linux one can delete files and directories that are in use, on windows one can't. That's why it doesn't work unmodified. - Leon Timmermans
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With