我可以使用通配符来监视 File::Monitor 目录中的特定文件吗?

can i use wild card to monitor particular files in a directory in File::Monitor?

我正在使用 File::Monitor perl 模块来通知我有关 new/modified 个文件的信息。

以下代码片段仅适用于 1.txt 输入文件!我想监视所有输入文件的位置。

我有 2 个问题

 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");
}
}
}

reading directory is ok! but a folder is created within this input directory and then if any *.txt file is created then ? i want to observed all .txt file inside my input directory

您可以为目录设置一个 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 将显示为修改后的文件。