Just for fun, here's an HTA version...
<html>
<title>WatchFolder</title>
<hta:application
id="WatchFolder" applicationname="WatchFolderHTA"
singleinstance="yes" windowstate="minimize"
caption="yes" showintaskbar="yes"
sysmenu="yes" scroll="yes"
/>
<object id="mysink" style="display:none;"
classid="clsid:75718C9A-F029-11D1-A1AC-00C04FB6C223"></object>
<script language="vbscript">
username=""
password=""
comp_name=""
set locator = CreateObject("Wbemscripting.SWbemLocator")
locator.security_.impersonationLevel = 3 'impersonate
set connector = _
locator.ConnectServer(Comp_Name,"root/cimv2",username,password)
wql = "Select * From __InstanceCreationEvent Within 2 " _
& "Where TargetInstance ISA 'cim_DirectoryContainsFile' " _
& "AND TargetInstance.GroupComponent=" _
& "'Win32_Directory.Name=""c:\\\\test""'"
connector.ExecNotificationQueryAsync mysink, wql
</script>
<script language="vbscript" for="mysink"
event="OnObjectReady(objObject, objAsyncContext)">
s = "[" & time() & "] "
set o = connector.get(objObject.targetinstance.Partcomponent)
s = s & o.name & "<br>"
output.insertAdjacentHTML "beforeEnd",s
</script>
<style>
body {
font:x-small 'courier new';
color:black;background-color:white;
}
</style>
</head>
<body>
<div id="output"></div>
</body>
</html>
--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US
--
Quote:
> my solution script on the basis of your script with async notification
> 'start script ------------
> username=""
> password=""
> comp_name=""
> set locator = CreateObject("Wbemscripting.SWbemLocator")
> locator.security_.impersonationLevel = 3 'impersonate
> set connector =
> locator.ConnectServer(Comp_Name,"root/cimv2",username,password)
> wql = "Select * From __InstanceCreationEvent Within 2 " _
> & "Where TargetInstance ISA 'cim_DirectoryContainsFile' " _
> & "AND TargetInstance.GroupComponent=" _
> & "'Win32_Directory.Name=""c:\\\\test""'"
> set sink = WScript.CreateObject("WbemScripting.SWbemSink","SINK_")
> connector.ExecNotificationQueryAsync sink, wql
> wscript.echo "wait for events"
> sink.Cancel()
> '--------------Event Function ----------
> Sub SINK_OnObjectReady(objObject, objAsyncContext)
> WScript.Echo "File Created" & objObject.targetinstance.Partcomponent
> set o = connector.get(objObject.targetinstance.Partcomponent)
> wscript.echo o.name
> End Sub
> ' end script -------------------------
> Ingo
> > > You can also write a script that uses WMI to register for a file system
> change notification event on a folder. I've seen examples but don't recall
> enough of the details to go out searching for one ;-)...
> > Adding WMI into the "soup" isn't going to save the OP any memory, is it
> ;-)
> > I didn't find any complete example, but I built one:
> > --------------------- Script start ---------------------
> > ' Script that will detect all new files in c:\test with a optmized query.
> > strQuery = "Select * From __InstanceCreationEvent Within 2 " _
> > & "Where TargetInstance ISA 'cim_DirectoryContainsFile' " _
> > & "AND TargetInstance.GroupComponent=" _
> > & "'Win32_Directory.Name=""c:\\\\test""'"
> > ' for remote, define a sComputerName variable and exchange the line
> > ' above with this two ( in addition to the changes in the
> > ' "GetObject("winmgmts:")" statement as well):
> > ' & "AND TargetInstance.GroupComponent='\\\\" & sComputerName _
> > ' & "\\root\\cimv2:Win32_Directory.Name=""c:\\\\test""'"
> > Set oEvents = GetObject("winmgmts:").ExecNotificationQuery (strQuery)
> > WScript.Echo "Waiting for events..."
> > ' now wait for events ...
> > Do While True ' loop indefinitely
> > Set oNTEvent = oEvents.NextEvent.TargetInstance ' retrieve event
> > WScript.Echo "Data: " & oNTEvent.PartComponent
> > Loop
> > ' Will give you an output on this format for each new file:
> > ' \\<computername>\root\cimv2:CIM_DataFile.Name="c:\\test\\New.txt"
> > --------------------- Script end ---------------------
> > To "kill" this script in a nice way, I recommend this procedure:
> > From a command prompt, run:
> > 1)
> > net stop "Windows Management Instrumentation"
> > 2)
> > net start "Windows Management Instrumentation"
> > Why should you do this, and not just kill cscript.exe/wscript.exe from the
> task list, you might ask?
> > Well, you could, but my experience is that WMI in that case a lot of times
> gets "crazy" and starts eating all my memory until my computer "jumps off a
> cliff".
> > --
> > torgeir