Perl新手,只用了3次。我需要从父目录中删除文件和子文件夹,当它们超过一周的时间。我在使用-M之前删除了文件,但从未使用子文件夹。当我运行下面的详细信息时,没有文件从子文件夹中删除,并且子文件夹中存在一周以上的文件。对于子文件夹中的所有文件,测试消息显示'myAge'为零。不知道我错过了什么。任何援助将非常感激。使用Perl删除子文件夹和文件#
msg ("\n");
msg ("Start: \n");
my $parent = 'C:/temp/XYZ';
my ($par_dir, $sub_dir);
opendir($par_dir, $parent);
msg " parent is $parent \n";
while (my $sub_folders = readdir($par_dir)) {
next if ($sub_folders =~ /^..?$/); # skip . and ..
my $path = $parent . '/' . $sub_folders;
next unless (-d $path); # skip anything that isn't a directory
next unless (-M $subfolder < 7 );
msg " subfolder is $sub_folders is old enough to delete \n";
opendir($sub_dir, $path);
while (my $file = readdir($sub_dir)) {
# for testing
my $myAge = (-M $file) ;
msg " age ... $myAge __ file ... $file\n" ;
if (-M $file > 7 ) {
msg " going to delete this file... $file \n";
} else {
msg " will keep this file not old enough $file\n";
}
}
closedir($sub_dir);
}
closedir($par_dir);
'msg()'函数来自哪里? – simbabque
除非(-M $ path <7);否则不应该为'next除非(-M $子文件夹<7);'为'而'-M $ file'是'-M“$ path/$ file”'? ['readdir'](http://perldoc.perl.org/functions/readdir.html)只返回一个文件或目录名(没有它的父目录)。 – PerlDuck
msg()写入日志文件,对不起,因为没有引用早期 – Fondah