【发布时间】:2014-05-07 11:33:47
【问题描述】:
我一直在尝试创建一个 perl 脚本来监视文件夹中创建的任何新子文件夹,并且必须将其复制到新位置。
到目前为止,我能够想出下面的代码。我可以监视和捕获新创建的文件夹,但我不确定如何等到在这个新创建的文件夹下创建所有子文件夹/文件。
我当前的代码是
use warnings;
use strict;
use Linux::Inotify2;
use File::Copy::Recursive qw(dircopy);
my $target = "/home/Interface/tmp";
print STDERR "Watching $target...\n";
my $in2 = Linux::Inotify2->new();
die "Inotify2: $!" if (! defined $in2 );
$in2->watch ($target, IN_CREATE) or die "watch: $!";
#### Need to do Something here to wait untill IN_CREATE EVENT finishes fully#######
while (1) {
foreach my $watchdog ($in2->read()) {
print "$watchdog->{name}\n ";
caputure_event($watchdog);
}
}
sub caputure_event
{
my ($itemlist) = @_;
my $itemlistPath = $itemlist->fullname;
my $itemlistID = $itemlist->{name};
copyfile($itemlistPath,$itemlistID);
$itemlistPath = "";
}
sub copyfile
{
my ($sourcedir,$itemlistName) = @_;
my $targetparentdir = "/home/Interface/Input";
my $targetdir = "$targetparentdir/$itemlistName";
dircopy("$sourcedir", "$targetdir");
}
我的问题是copyfile 函数在新目录下创建所有文件之前执行。我想获得专家的想法来解决这个问题。我可以使用sleep 函数,但认为效率不高。
【问题讨论】:
-
嗨!我之前问过这个问题:stackoverflow.com/questions/21476798/… 它适用于 Win32,但该解决方案也应该适用于您。
-
@capfan 这不是一个坏主意,但我的问题是文件不止一个,而且文件的数量根据不同的用例而有所不同。我在这里想别的东西......如果你想到其他想法,请分享它。谢谢
-
您可以定期检查文件夹内所有文件的更改。这是一项昂贵的操作,但由于操作系统确实(可能)不提供此信息,这可能是您唯一的机会。但请记住,对于网络副本,最后更改的信息可能不可靠。
标签: perl