我正在从http://www.perlmonks.org/index.pl?node_id=217166转换一个 linux 脚本,具体如下:
#!/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);
我的尝试是:
#!/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);
但是,当脚本尝试删除一个目录时,我收到一个错误,指出该目录正在被另一个进程使用(当它不是时)。有任何想法吗?我正在使用 ActiveState 5.10 在 Windows Server 2003 SP2 64 位上运行该脚本。
谢谢!