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