使用Perl等待文件进入文件夹并将其移动到其他位置

时间:2012-08-15 04:45:24

标签: perl file wait

我对Perl很新,并且想要了解如何使用Perl来检测文件是否已填充文件夹,然后将这些文件移动到其他位置。

我见过许多代码等待特定文件的代码,但我的公司对文件使用了数字随机函数,因此没有两个文件具有相同的名称。

感谢您对此事的任何帮助。

再次感谢。

亚伦

1 个答案:

答案 0 :(得分:1)

更新:@daxim在评论中建议File::ChangeNotify是一个跨平台模块,其工作方式与Win32 :: FileSystem :: Watcher非常相似。

快速搜索CPAN建议Win32::FileSystem::Watcher可以提醒您目录中的更改。您需要安装此模块和任何依赖项。

来自文档:

use Win32::FileSystem::Watcher;

my $watcher = Win32::FileSystem::Watcher->new( "c:\\" );

# or

my $watcher = Win32::FileSystem::Watcher->new(
    "c:\\",
    notify_filter  => FILE_NOTIFY_ALL,
    watch_sub_tree => 1,
);

$watcher->start();
print "Monitoring started.";

sleep(5);

# Get a list of changes since start().
my @entries = $watcher->get_results();

# Get a list of changes since the last get_results()
@entries = $watcher->get_results();

# ... repeat as needed ...

$watcher->stop(); # or undef $watcher

foreach my $entry (@entries) {
    print $entry->action_name . " " . $entry->file_name . "\n";
}

# Restart monitoring

# $watcher->start();
# ...
# $watcher->stop();