hi all,
I need a script to monitor a folder and popup a message when new file are created. Found the following WMI script but unable to convert to Winbatch. Could someone please help me. Thank you.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""c:\\\\scripts""'")
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
Wscript.Echo objLatestEvent.TargetInstance.PartComponent
Loop
The code looks much the same. Here is the WinBatch code:
strComputer = "."
objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" : strComputer : "\root\cimv2")
colMonitoredEvents = objWMIService.ExecNotificationQuery (`SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent= 'Win32_Directory.Name="c:\\\\temp"'`)
While @True
objLatestEvent = colMonitoredEvents.NextEvent
Pause("", objLatestEvent.TargetInstance.PartComponent)
EndWhile
hi Deana,
Is possible to refine the script so that only file such as "Doc*.PDF" or "FAX*.PDF" is detected? eg.DOC0012.PDF
How to display just the filename without the entire path from root?
Thank you very much, for your assistance.
I would probably query the CIM_DataFile Class instead so my query could include the Extension property:
Reference: http://msdn.microsoft.com/en-us/library/aa387236(v=vs.85).aspx
;***************************************************************************
;** Directory Watcher
;**
;** Purpose: Watch a Directory for a File with specific Extension
;** Inputs:
;** Outputs: Results displayed to screen.
;** Reference:
;**
;**
;** Developer: Deana Falk 2014.02.21
;***************************************************************************
title = 'Directory Watcher'
intInterval = "5" ; How often to check for changes
strDrive = "C:" ; Drive letter
strFolder = "\\Temp\\" ; Folder path using double slashes for WMI Query
strExt = "doc" ; File Extenstion of file to monitor
strComputer = "." ; . Indicates local computer
; Connect to WMI
objWMIService = GetObject( "winmgmts:{impersonationLevel=impersonate}!\\" : strComputer : "\root\cimv2" )
; The WMI Query string
strQuery = "Select * From __InstanceOperationEvent Within " : intInterval : " Where Targetinstance Isa 'CIM_DataFile' And TargetInstance.Drive='" : strDrive : "' And TargetInstance.Path='" : strFolder : "' AND TargetInstance.Extension='":strExt:"'"
; Execute the query
colMonitoredEvents = objWMIService.ExecNotificationQuery ( strQuery )
; Loop waiting for the next event
While @TRUE
objLatestEvent = colMonitoredEvents.NextEvent
class = objLatestEvent.Path_.Class
name = objLatestEvent.TargetInstance.Name
Select @TRUE
Case class == "__InstanceCreationEvent"
Pause("A new file was just created:", name )
Break
Case class == "__InstanceDeletionEvent"
Pause("A file was just deleted:", name )
Break
;Case class == "__InstanceModificationEvent"
; objPrevInst = objLatestEvent.PreviousInstance
; ForEach objProperty In objTargetInst.Properties_
; If objProperty.Value <> objPrevInst.Properties_(objProperty.Name)
; Pause("A file was just modified:", "Changed:":name:@LF:"Property: ":objProperty.Name:@LF:"Previous value: ":objPrevInst.Properties_(objProperty.Name):@LF:"New value: ":objProperty.Value)
; EndIf
; Next
; Break
EndSelect
EndWhile
Pause(title, 'Complete')
Exit
hi Deana,
Is possible to specify a file mask instead of extension ?
I would like to detect only new PDF files that match a file mask such as Doc*.PDF. Examples of file name - Doc20140221001.PDF or Fax20140221002.PDF
Thank you very much.
Maybe using the LIKE operator. The LIKE operator is valid in the WHERE clause and is used to determine whether a given character string matches a specified pattern.
Here is a good resource that should help get you started coding your query: http://msdn.microsoft.com/en-us/library/aa392902%28v=vs.85%29.aspx
I make a little change to your script, to display only new PDF files with Doc*.PDF & Fax*.PDF
While @TRUE
objLatestEvent = colMonitoredEvents.NextEvent
class = objLatestEvent.Path_.Class
name = objLatestEvent.TargetInstance.Name
Select @TRUE
Case class == "__InstanceCreationEvent"
bb=StrUpper(StrSub(name,9,3))
cc=StrSub(name,9,StrLen(name)-8)
if bb=="DOC" || bb=="FAX"
Display(3,"A new file was just created:",cc)
endif
Break
You had been a great help. Thank you very much.