Jump to content
Sign in to follow this  

TrayItemSetOnEvent - Exit script alternatives

Recommended Posts


I wrote a script that is processing files.

What I am basically doing is to monitor for event creation (see below):


$objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")

$colMonitoredEvents = $objWMIService.ExecNotificationQuery _

("SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE " _

& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _

& "TargetInstance.GroupComponent= " _

& "'Win32_Directory.Name=""" & $SourceFolder & """'")

followed by an infinite loop

While 1

$objLatestEvent = $colMonitoredEvents.NextEvent

$FileName2 = $objLatestEvent.TargetInstance.PartComponent

; ....

; code for processing the files

; ....



The problem is that I cannot find a way to terminate this script -other than killing the task.

I tried to use something like this:



$exititem = TrayCreateItem("Exit")



Func ExitScript()




But this code will work only after I will drop a new file in the source folder ($SourceFolder).

Are there any other ways I can exit the script?



Share this post

Link to post
Share on other sites

You could always register a hotkey at the top of the script.


Thank you very much for your reply.

I tried and I got the same result.

The script will stop and wait for the event (new file)

on line 2 (see below)

1 While 1

2 $objLatestEvent = $colMonitoredEvents.NextEvent

3 $FileName2 = $objLatestEvent.TargetInstance.PartComponent

4 ...

5 WEnd

It will exit only if I will drop a file in that folder.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...