我正在使用File :: Monitor perl模块通知我有关新/修改过的文件。
以下代码段仅适用于1.txt输入文件!我希望监视所有输入文件的位置。
我有两个问题
1-> Does it support wild card such as *.txt?
2-> Does it monitor recursively ?
这是代码段。
#!/usr/bin/perl
use strict;
use warnings;
use File::Monitor;
use File::Monitor::Object;
my @Files=("1.txt","2.txt");
my $dir = "C:\\Users\\goudarsh\\Desktop\\Perl_test_scripts";
my $wFileMonitor = File::Monitor->new();
foreach my $wFile (@Files){
my $file = "$dir\\$wFile";
$wFileMonitor->watch($file);
#-- First scan does nothing
$wFileMonitor->scan;
while (1){
my @changes = $wFileMonitor->scan;
foreach my $object (@changes) {
my $modified = $object->mtime;
print "$wFile changed\n";
#fcopy ("$wFile","c:\\newpath");
}
}
}
答案 0 :(得分:1)
阅读目录没问题!但是在此输入中创建了一个文件夹 目录然后如果创建任何* .txt文件呢?我想要 观察了我输入目录中的所有.txt文件
您可以为目录设置recurse
选项,该选项将监视所有子目录中的更改:
$monitor->watch( {
name => '/Users/7stud/pperl_programs/test_dir/',
recurse => 1,
callback => {files_created => \&textfile_notifier}, #event => handler
} );
以下是跟踪.txt
文件创建的方法:
use strict;
use warnings;
use 5.020;
use File::Monitor;
use File::Basename;
sub textfile_notifier {
my ($watch_name, $event, $change) = @_;
my @new_file_paths = $change->files_created; #The change object has a property called files_created,
#which contains the names of any new files
for my $path (@new_file_paths) {
my ($base, $fname, $ext) = fileparse($path, '.txt'); # $ext is "" if the '.txt' extension is
# not found, otherwise it's '.txt'.
if ($ext eq '.txt') {
say "$path was created";
}
}
}
my $monitor = File::Monitor->new();
$monitor->watch( {
name => '/Users/7stud/pperl_programs/test_dir/',
recurse => 1,
callback => {files_created => \&textfile_notifier}, #event => handler
} );
$monitor->scan;
while (1) {
$monitor->scan;
sleep(2);
}
侦听对目录中所有现有文件的修改;还会侦听目录中创建的文件,并监听对创建文件的后续修改:
use strict;
use warnings;
use 5.020;
use File::Monitor;
use File::Basename;
use File::Find;
#Gather all existing .txt files:
my @existing_textfiles;
sub textfiles {
my ($base, $fname, $ext) = fileparse($File::Find::name, '.txt');
if ($ext eq '.txt') {
push @existing_textfiles, $File::Find::name;
}
}
find(\&textfiles, '/Users/7stud/pperl_programs/test_dir/');
#------------------------------
#Watch for modifications to existing text files:
my $monitor = File::Monitor->new();
for my $existing_textfile (@existing_textfiles) {
$monitor -> watch( {
name => $existing_textfile,
callback => { mtime => \&textfile_modified },
} );
}
sub textfile_modified {
my ($modified_file) = @_;
say "modified: $modified_file";
}
#------------------------------
#Watch for created text files:
$monitor->watch( {
name => '/Users/7stud/pperl_programs/test_dir/',
recurse => 1,
callback => {files_created => \&textfile_created},
} );
sub textfile_created {
my ($watch_dir, $event, $change) = @_;
my @new_files = $change->files_created;
for my $new_file (@new_files) {
my ($base, $fname, $ext) = fileparse($new_file, '.txt');
if ($ext eq '.txt') {
say "created: $new_file";
#Watch for moditications to created text files:
$monitor->watch( {
name => $new_file,
callback => { mtime => \&textfile_modified },
} );
#------------------------
}
}
}
#---------------------------
$monitor->scan;
while (1) {
$monitor->scan;
sleep(2);
}
请注意,deleting a file
将显示为已修改的文件。