未命名  

需要安裝的模組:
File::Monitor
File::Find::Rule
安裝方法:
win下(需要有perl環境,我這裡裝的是activeperl)
cmd下進入c:\perl\bin
輸入ppm-shell
ppm> install File::Monitor File::Find::Rule
 
 
 

use strict;
use warnings;
use Data::Dumper;
use File::Monitor;
use File::Monitor::Delta;
use File::Monitor::Object;
use File::Find::Rule;
use POSIX qw(strftime);
#my @files=File::Find::Rule->not_name('.*.swp')->in('C:\perl','d:\24'); #這裡可以寫多個目錄,我這裡做測試是用win,linux同理
START:
my @files=File::Find::Rule->not_name('.*.swp')->in('/home/mcshell/cu');
my $monitor=File::Monitor->new();
foreach my $file (@files){
$monitor->watch({
name => "$file",
recurse => 1, #是否遞迴目錄
}
);
}
while(1 and sleep 3) { #每一秒檢測一次
my @changes=$monitor->scan;
open FH,'>>c:\Perl\monitor.log' or die "$!"; #將變動的檔寫入日誌
#open FH,'>>/var/log/monitor.log' or die "$!"; #linux與win的路徑
for my $change (@changes) {
my $name = $change->name;
my @created = $change->files_created;
my @deleted = $change->files_deleted;
if (@deleted){
}
if(@created){
print FH "@created The created\n"; #如果有新檔則重新索引檔
goto START;
}
if($change->is_mtime){ #判斷是否為修改檔
print FH $change->name."======>";
if ($change->is_size ) { #如果大小變化則判斷變化多少
my $old_size = ($change->old_size)||0;
my $new_size = ($change->new_size)||0;
print FH "has changed size from $old_size to $new_size"."====>";
}
my $time = $change->new_mtime;
if(defined $time){
my $mtime = strftime("%Y-%m-%d %H:%M:%S\n", localtime($time));
print FH "$mtime";
}
}
}
close FH;
}

 

 

 

本文出自 “mcshell学习博客” 博客,请务必保留此出处http://mcshell.blog.51cto.com/803455/1080563

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()