简体   繁体   English

如何在将文件添加到目录时运行Windows批处理或Perl脚本?

[英]How can I get a Windows batch or Perl script to run when a file is added to a directory?

I am trying to write a script that will parse a local file and upload its contents to a MySQL database. 我正在尝试编写一个脚本来解析本地文件并将其内容上传到MySQL数据库。 Right now, I am thinking that a batch script that runs a Perl script would work, but am not sure if this is the best method of accomplishing this. 现在,我认为运行Perl脚本的批处理脚本可以工作,但我不确定这是否是实现此目的的最佳方法。

In addition, I would like this script to run immediately when the data file is added to a certain directory. 另外,我希望在将数据文件添加到某个目录时立即运行此脚本。 Is this possible in Windows? 这在Windows中可行吗?

Thoughts? 思考? Feedback? 反馈? I'm fairly new to Perl and Windows batch scripts, so any guidance would be appreciated. 我是Perl和Windows批处理脚本的新手,所以任何指导都会受到赞赏。

You can use Win32::ChangeNotify . 您可以使用Win32 :: ChangeNotify Your script will be notified when a file is added to the target directory. 将文件添加到目标目录时,将通知您的脚本。

It wouldn't be too hard to put together a small C# application that uses the FileSystemWatcher class to detect files being added to a folder and then spawn the required script. 将一个使用FileSystemWatcher类的小型C#应用程序放在一起来检测添加到文件夹中的文件,然后生成所需的脚本并不难。 It would certainly use less CPU / system resources / hard disk bandwidth than polling the folder at regular intervals. 与定期轮询文件夹相比,它肯定会使用更少的CPU /系统资源/硬盘带宽。

Checking a folder for newly created files can be implemented using the WMI functionality. 可以使用WMI功能实现检查新创建文件的文件夹。 Namely, you can create a Perl script that subscribes to the __InstanceCreationEvent WMI event that traces the creation of the CIM_DirectoryContainsFile class instances. 也就是说,您可以创建一个Perl脚本,该脚本订阅__InstanceCreationEvent WMI事件,该事件跟踪CIM_DirectoryContainsFile类实例的创建。 Once that kind of event is fired, you know a new file has been added to the folder and can process it as you need. 一旦触发了这种事件,您就知道文件夹中添加了一个新文件,可以根据需要对其进行处理。

These articles provide more information on the subject and contain VBScript code samples (hope it won't be hard for you to convert them to Perl): 这些文章提供了有关该主题的更多信息,并包含VBScript代码示例(希望您将它们转换为Perl并不困难):

The function you want is ReadDirectoryChangesW . 你想要的功能是ReadDirectoryChangesW A quick search for a perl wrapper yields this Win32::ReadDirectoryChanges module. 快速搜索perl包装器会生成此Win32 :: ReadDirectoryChanges模块。

Your script would look something like this: 你的脚本看起来像这样:

use Win32::ReadDirectoryChanges;

$rdc = new Win32::ReadDirectoryChanges(path    => $path,
                                       subtree => 1,
                                       filter  => $filter);

while(1) {
    @results = $rdc->read_changes;

    while (scalar @results) {
      my ($action, $filename) = splice(@results, 0, 2);
      ... run script ...
    }
}

You can easily achieve this in Perl using File::ChangeNotify . 您可以使用File::ChangeNotify在Perl中轻松实现此目的。 This module is to be found on CPAN: http://search.cpan.org/dist/File-ChangeNotify/lib/File/ChangeNotify.pm 该模块可在CPAN上找到: http//search.cpan.org/dist/File-ChangeNotify/lib/File/ChangeNotify.pm

You can run the code as a daemon or as a service, make it watch one or more directories and then automatically execute some code (or start up a script) if some condition matches. 您可以将代码作为守护程序或服务运行,使其监视一个或多个目录,然后在某些条件匹配时自动执行某些代码(或启动脚本)。

Best of all, it's cross-platform, so should you want to switch to a Linux machine or a Mac, it would still work. 最重要的是,它是跨平台的,所以如果你想切换到Linux机器或Mac,它仍然可以工作。

You need to consider what is a sufficient heuristic for determining "modified". 你需要考虑什么是足够的启发式来确定“修改”。

In increasing order of cost and accuracy: 按成本和准确度递增顺序:

  1. file size (file content can still be changed as long as size is maintained) 文件大小(只要保持大小,文件内容仍然可以更改)

  2. file timestamp (If you aren't running ntpd time is not monotonic) 文件时间戳(如果你没有运行ntpd时间不是单调的)

  3. file sha1sum (bulletproof but expensive) 文件sha1sum(防弹但价格昂贵)

I would run ntpd, and then loop over the timestamps, and then compare the checksum if the timestamp changes. 我将运行ntpd,然后遍历时间戳,然后比较时间戳更改时的校验和。 This can cover a lot of ground in little time. 这可以在很短的时间内覆盖很多地方。

These methods are not appropriate for a computer security application, they are for file management on a sane system. 这些方法不适用于计算机安全应用程序,它们用于理智系统上的文件管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM