WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

WMI
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Directory Watcher

 Keywords:  Directory Watcher Watch DirectoryFile with Specific Extension ExecNotificationQuery Targetinstance Isa CIM_DataFile __InstanceCreationEvent __InstanceDeletionEvent 

;***************************************************************************
;**         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



Article ID:   W18470
Filename:   Directory Watcher.txt
File Created: 2014:02:21:12:32:44
Last Updated: 2014:02:21:12:32:44