我需要删除给定文件夹下的所有内容(文件和文件夹)。问题是该文件夹中有数百万个文件和文件夹。所以我不想一次性加载所有文件名。
逻辑应该是这样的:
- 迭代文件夹而不加载所有内容
- 获取文件或文件夹
- 删除它 (详细说明文件或文件夹“X”已被删除)
- 去下一个
我正在尝试这样的事情:
sub main(){
my ($rc, $help, $debug, $root) = ();
$rc = GetOptions ( "HELP" => \$help,
"DEBUG" => \$debug,
"ROOT=s" => \$root);
die "Bad command line options\n$usage\n" unless ($rc);
if ($help) { print $usage; exit (0); }
if ($debug) {
warn "\nProceeding to execution with following parameters: \n";
warn "===============================================================\n";
warn "ROOT = $root\n";
} # write debug information to STDERR
print "\n Starting to delete...\n";
die "usage: $0 dir ..\n" unless $root;
*name = *File::Find::name;
find \&verbose, @ARGV;
}
sub verbose {
if (!-l && -d _) {
print "rmdir $name\n";
} else {
print "unlink $name\n";
}
}
main();
它工作正常,但每当“查找”读取大文件夹时,应用程序就会卡住,我可以看到 Perl 的系统内存增加直到超时。为什么?它是否试图一次性加载所有文件?
谢谢你的帮助。