Windows – 使用perl监视一个目录下的新文件的删除/创build

寻找一种方法来监视一个新的文件创build或删除的目录。

所以如果我有一个文件夹c:\ temp,如果在这个复制/创build一个abc.txt我想要一个事件或一些东西,使我可以拿起该文件,然后处理它。

另外,我想连续监视这个文件夹。 我怎样才能做到这一点。 我正在写一个这样做的服务。 我想将监视和处理合并到一个脚本中。

提前致谢。

答案在这里: 在Perl中,我怎样才能看到一个目录的变化?

对于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

 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 } = (); }