How can I get a Windows package or Perl script to run when a file is added to the directory?

I am trying to write a script that will parse a local file and load its contents into a MySQL database. Right now, I think the script package that runs the Perl script will work, but I'm not sure if this is the best way to execute this.

In addition, I would like this script to be run immediately when the data file is added to a specific directory. Is this possible on Windows?

Thoughts? Feedback? I am new to Perl and Windows scripts, so any recommendations would be appreciated.

+4
source share
6 answers

You can use Win32 :: ChangeNotify . Your script will be notified when the file is added to the target directory.

+9
source

It's not so difficult to build a small C # application that uses the FileSystemWatcher class to detect files added to a folder and then spawn the required script. This, of course, will use less CPU / system resources / hard drive bandwidth than polling folders at regular intervals.

+1
source

Folder verification for newly created files can be implemented using WMI . Namely, you can create a Perl script that subscribes to the __InstanceCreationEvent WMI event that monitors the creation of the CIM_DirectoryContainsFile . As soon as this event is fired, you know that a new file has been added to the folder and can process it as needed.

These articles provide additional information on this subject and provide examples of VBScript code (I hope you can easily convert them to Perl):

+1
source

The function you want is ReadDirectoryChangesW . A quick search for the perl shell is provided by this Win32 :: ReadDirectoryChanges module .

Your script will 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 ... } } 
+1
source

You can easily achieve this in Perl using File::ChangeNotify . This module can be found in 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 look at one or several directories, and then automatically execute some code (or run a script) if any condition is met.

Best of all, it's cross-platform, so if you want to switch to a Linux machine or Mac, it will still work.

+1
source

You need to consider what is sufficient heuristic to define "modified."

In order of increasing cost and accuracy:

  • file size (file contents can still be resized as long as the size is supported)

  • file timestamp (if you are not using ntpd, time is not monotonous)

  • sha1sum file (bulletproof but expensive)

I will run ntpd and then scroll through the timestamps and then compare the checksum if the timestamp changes. It can cover a lot of land in a short time.

These methods are not suitable for a computer security application; they are designed to manage files in a smart system.

0
source

All Articles