Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

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?

Thoughts? Feedback? I'm fairly new to Perl and Windows batch scripts, so any guidance would be appreciated.

like image 768
indiguy Avatar asked Dec 13 '22 02:12

indiguy


2 Answers

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

like image 163
Sinan Ünür Avatar answered Dec 22 '22 00:12

Sinan Ünür


Checking a folder for newly created files can be implemented using the WMI functionality. Namely, you can create a Perl script that subscribes to the __InstanceCreationEvent WMI event that traces the creation of the CIM_DirectoryContainsFile class instances. 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):

  • How Can I Automatically Run a Script Any Time a File is Added to a Folder?
  • WMI and File System Monitoring
like image 44
Helen Avatar answered Dec 21 '22 23:12

Helen