寻找一种方法来监视目录中新文件的创建或删除。
因此,如果我有一个文件夹 c:\temp 并且在其中复制/创建了 abc.txt,我需要一个事件或其他内容,以便我可以拾取该文件然后处理它。
另外,我想持续监控这个文件夹。我怎样才能做到这一点。我正在编写一个服务来完成这一切。我想将监视和处理合并到一个脚本中。
提前致谢。
答案在这里:在 Perl 中,如何查看目录的更改? https://stackoverflow.com/questions/11251666/in-perl-how-can-i-watch-a-directory-for-changes
对于Linux:
use File::ChangeNotify;
my $watcher = File::ChangeNotify->instantiate_watcher(
directories => [ 'archive/oswiostat' ],
filter => qr/\Aoracleapps[.].*dat\z/,
);
while (my @events = $watcher->wait_for_events) {
# ...
}
我认为你使用的是 Windows,所以你必须使用 Win32::ChangeNotify
示例来自:http://www.perlmonks.org/?node_id=306175 http://www.perlmonks.org/?node_id=306175
use strict;
use Win32::ChangeNotify;
our $PATH ||= '.';
our $S = defined $S ? 1 : 0;
my $notify = Win32::ChangeNotify->new( $PATH, $S, 'FILE_NAME' );
my %last; @last{ glob $PATH . '/*' } = ();
while( 1 ) {
print('Nothing changed'), next
unless $notify->wait( 10_000 ); # Check every 10 seconds
$notify->reset;
print 'Something changed';
my @files = glob $PATH . '/*';
if( @files < scalar keys %last ) {
delete @last{ @files };
print 'These files where deleted: ';
print for keys %last;
}
elsif( @files > scalar keys %last ) {
my %temp;
@temp{ @files } = ();
delete @temp{ keys %last };
print 'These files where created: ';
print for keys %temp;
}
else {
print "A non-deletion or creation change occured";
}
undef %last;
@last{ @files } = ();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)