Steve and David,

This was a great idea, but the original author had some errors in his
example code (looks like he took code from an async handler and stuffed it
into his sync example, but the objects you're dealing with either aren't the
same, or he just had errors).

Anyway, below is a working example I had done by end of the day yesterday,
which watches a specific directory for new files, using the synchronous form
of WMI calls (and WE's form of VBS converted from his WSH).  It appears to
me that WE still will not work with WMI asynchronous calls (this was
something I reported long ago), which is unfortunate, but  you can get
around it using this form, which polls every 5 seconds:

Option Explicit
' WE script example showing how to monitor a directory (using WMI) for file
additions

queue "monitor_dir", "\chip\"



Sub monitor_dir(ForFolder)
' uses WMI to monitor a specific directory for file additions

Dim strDrive, strFolder, strComputer, intInterval, strQuery, response,
filename
Dim objWMIService
Dim colMonitoredEvents
Dim objLatestEvent
Dim objTarget

strDrive = "C:"
' folder name must have directories separated with double back slashes
strFolder = Replace(ForFolder, "\", "\\")
strComputer = "."
intInterval = "5" ' seconds


' Connect WMI service
Set objWMIService = GetObject("winmgmts:"  &
"{impersonationLevel=impersonate}!\\" &  strComputer & "\root\cimv2")
DoEvents
loadClassInformation objWMIService

' build query
strQuery = "Select * From __InstanceCreationEvent "  & " Within " &
intInterval  & " Where Targetinstance Isa 'CIM_DataFile'"  & " And
TargetInstance.Drive='" & strDrive & "'"  & " And TargetInstance.Path='" &
strFolder & "'"

' should also be able to use  __InstanceDeletionEvent for file deletions?


' Execute notification query
Set colMonitoredEvents = objWMIService.ExecNotificationQuery(strQuery)
DoEvents


Do
' wait for an event
Set objLatestEvent = colMonitoredEvents.NextEvent()
DoEvents

' to see structure of received object uncomment below
' response = objLatestEvent.GetObjectText_()
' MsgBox response, vbOKOnly + vbInformation + vbMsgBoxSetForeground,
"received object"

response = ""
Set objTarget = objLatestEvent.targetInstance
response = objTarget.filename ' no path
' .name contains name with path, and .path contains only  path

' now do something with the file name
queue "SpeakIt", "received " & response
Set objLatestEvent = Nothing
DoEvents
Loop

End Sub


Sub speakIt(msg)

Speak msg


End Sub




-----Original Message-----
From: Steve Clower [mailto:sclo...@aisquared.com] 
Sent: Thursday, January 22, 2015 9:23 AM
To: gw-scripting@gwmicro.com
Subject: Re: Think I could need a bit of help...

You might also take a look at the approach which takes advantage of WMI 
described more here: 
http://stackoverflow.com/questions/760904/how-can-i-monitor-a-windows-direct
ory-for-changes.

Steve


On 1/22/2015 9:08 AM, LBX wrote:
> Dave,
>      This will check it at the 5 minute mark.
>
> Sample Format:
>    StartTimer 250, "SetFocus", dObj.Control( dID )
> Your Format:
>    StartTimer 50000, "CheckForState"
> Sub CheckForState()
> End Sub
>
> Sent: Thursday, January 22, 2015 6:05 AM
> Subject: Think I could need a bit of help...
>
>
> Scripters,
> In one of my projects, I need some routine to check a certain state of
> things, with given intervals. This should be done in the background,
> with as little interference with the user's activity, as possible. Let's
> for instance say, you want to check if a file has been updated, and you
> want to do that every 5 minutes, all through the day.
>
> OK, I have one idea, but wanted to know if the WE API gives a better
> solution, and if so, could someone please point me to some code or the
> like, that illustrates how to implement. Here is the idea I have, but
> not sure if it is all that waterproof, or if it holds any chance of
> malfunctioning the app.
>
> Sub CheckForState()
>       Sleep 5000     'Let's just give it five seconds interval.
>       ' code activity goes here...
>       CheckForState()
> End Sub 'ChackForState.
>
> In other words, a simple, recursive sub, that will keep running all till
> the app closes. Yet, what if I need to end it from running, at any given
> state throughout the app execution? Like I said, not sure if this is the
> best and most smooth way to handle this task, so if anyone has a better
> idea, I am all ears.
>
> Thanks,
>

Reply via email to