'/////////////////////////////////////////////////////////////////////////////////

' Author: SYO

' Copyright (c) 2015

'

' Purpose: Watch any change notification event from items (files and subfolders)

' into a directory so a trigger program can be started with this event.

'

'/////////////////////////////////////////////////////////////////////////////////

 

Const targetDirectory = "C:\APIDev"

Dim changeCounter

Dim ScriptEZ

 

Main()

 

Sub Main()

    Set ScriptEZ = WScript.CreateObject("ScriptEZ.API","ScriptEZ_")

    ScriptEZ.AppRegister

    changeCounter = 0

    notificationHandle = ScriptEZ.WaitForDirectoryChangeNotification(targetDirectory,0,0)

    If notificationHandle > 0 Then

        hThreadPool = ScriptEZ.CreateTaskQueueObject(1)

        r = ScriptEZ.AddTask(hThreadPool,notificationHandle)

    End If

 

    Do while (true)

        Wscript.Sleep 1000

    Loop

End Sub

 

Function ScriptEZ_onRun(localStorage)

    ‘ Waiting and yielding control every 7 seconds

    r = ScriptEZ.WaitForDirectoryChangeNotification("",localStorage,7)

    If r > 0 And changeCounter = 0 Then

      ScriptEZ.Echo CStr(Time) + " - onChange : " + CStr(changeCounter)

      changeCounter = 1

    ElseIf r = 0 And  changeCounter > 0 Then

         ScriptEZ.Echo CStr(Time) +  " - Changes occurred into " + targetDirectory + _

                                     " => triggering a program"

         '-- running a program here...

         changeCounter = 0

    End If

    ScriptEZ_onRun = CLng(1) '—- run again and again...

   

End Function