如果不管我的理解是正確的,你應該這樣進行:
閱讀與目錄名文件,並存儲到數據結構(在我的情況下,哈希值)。
閱讀目錄並從那裏過濾出文件夾。
- 迭代目錄並跳過文本文件中的文件夾並刪除超過30天的其他文件夾。
總體而言,這可以歸納爲如下Perl腳本:
注意:測試保存自己從unintensional數據丟失時評論rmdir $folder;
。
#!/usr/bin/perl
use strict;
use warnings;
use File::stat;
use Time::Piece;
#pass your file containing directory-name through command-line
my $file_with_dir=$ARGV[0];
my %dirs;
#open your file to check directories which shouldn't be removed
open my $fh, '<', $file_with_dir or die "unable to open file $file_with_dir: $!\n";
while(<$fh>){
chomp;
$dirs{$_}=1;
}
close($fh);
#read the folders from directory
my $dir=<your folder name here>;
opendir(my $dh,$dir) or die "unable to open DIR $dir: $!\n";
my @folders=grep{ !/^\./ && -d $_} readdir $dh;
closedir($dh);
#get today's date and time
my $curr_time=localtime;
foreach my $folder(@folders)
{
next if $dirs{$folder}; #skip if folder-name is in file you provided
my $stat=stat($folder);
my $creation_time=localtime($stat->ctime) if($stat);
#calculate difference in days
my $diff_days=int(($curr_time-$creation_time)->days);
if($diff_days > 30){
print "$folder is more than $diff_days days old deleting...\n";
#delete the folder use with caution comment it when you are testing
rmdir $folder or warn "unable to remove dir $folder: $!\n";
}
}
更新:
grep
通常用於濾除從列表中的元素。我過濾掉這符合兩項指標分析
!/^\./
這意味着名單不應該與.
開始的任何元素,如目錄可能包含.
和..
的元素。
-d
用於檢查項目是否是目錄(在你的情況下它是文件夾)。
這不是Perl代碼。 – serenesat
'FORFILES/S/D -30/C「cmd/c IF @isdir == TRUE rd/S/Q @path」' –
你好serenesat,你可以在perl下試試,但我已經在bat中試過了 – tilakraj