Go Back   TechArena Community > Technical Support > Computer Help > Windows Server > Windows Server Help
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Monitor folder for file modification, creation or deletion using WMI

Windows Server Help


Reply
 
Thread Tools Search this Thread
  #1  
Old 01-08-2007
Highlander
 
Posts: n/a
Monitor folder for file modification, creation or deletion using WMI

Hello all.

I've got a VBScript that will monitor a folder for any file
modification, creation or deletion. The script detects file
modifications just fine; but it does NOT detect any file creation or
deletion.

Any suggestions why this is occurring would be greatly appreciated.
Thanks!

- Dave

sPath = "\\Server\Drive$\Folder1\Folder2\Folder3"
sComputer = split(sPath,"\")(2)
sDrive = split(sPath,"\")(3)
sDrive = REPLACE(sDrive, "$", ":")
sFolders = split(sPath,"$")(1)
sFolders = REPLACE(sFolders, "\", "\\") & "\\"

Set objWMIService = GetObject("winmgmts:\\" & sComputer & "\root
\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE " _
& "TargetInstance ISA 'CIM_DataFile' AND " _
& "TargetInstance.Drive='" & sDrive & "' AND " _
& "TargetInstance.Path='" & sFolders & "'")

Wscript.Echo vbCrlf & Now & vbTab & _
"Begin Monitoring for a Folder Change Event..." & vbCrlf

Do
Set objLatestEvent = colMonitoredEvents.NextEvent
Select Case objLatestEvent.Path_.Class

Case "__InstanceCreationEvent"
WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName
_
& " was created" & vbCrlf

Case "__InstanceDeletionEvent"
WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName
_
& " was deleted" & vbCrlf

Case "__InstanceModificationEvent"
If objLatestEvent.TargetInstance.LastModified <> _
objLatestEvent.PreviousInstance.LastModified then
WScript.Echo Now & vbTab &
objLatestEvent.TargetInstance.FileName _
& " was modified" & vbCrlf
End If

IF objLatestEvent.TargetInstance.LastAccessed <> _
objLatestEvent.PreviousInstance.LastAccessed then
WScript.Echo Now & vbTab &
objLatestEvent.TargetInstance.FileName _
& " was accessed" & vbCrlf
End If

End Select
Loop

Set objWMIService = nothing
Set colMonitoredEvents = nothing
Set objLatestEvent = nothing


Reply With Quote
  #2  
Old 02-08-2007
urkec
 
Posts: n/a
RE: Monitor folder for file modification, creation or deletion using W

"Highlander" wrote:

> Hello all.
>
> I've got a VBScript that will monitor a folder for any file
> modification, creation or deletion. The script detects file
> modifications just fine; but it does NOT detect any file creation or
> deletion.
>
> Any suggestions why this is occurring would be greatly appreciated.
> Thanks!
>
> - Dave
>
> sPath = "\\Server\Drive$\Folder1\Folder2\Folder3"
> sComputer = split(sPath,"\")(2)
> sDrive = split(sPath,"\")(3)
> sDrive = REPLACE(sDrive, "$", ":")
> sFolders = split(sPath,"$")(1)
> sFolders = REPLACE(sFolders, "\", "\\") & "\\"
>
> Set objWMIService = GetObject("winmgmts:\\" & sComputer & "\root
> \cimv2")
> Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
> ("SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE " _
> & "TargetInstance ISA 'CIM_DataFile' AND " _
> & "TargetInstance.Drive='" & sDrive & "' AND " _
> & "TargetInstance.Path='" & sFolders & "'")
>
> Wscript.Echo vbCrlf & Now & vbTab & _
> "Begin Monitoring for a Folder Change Event..." & vbCrlf
>
> Do
> Set objLatestEvent = colMonitoredEvents.NextEvent
> Select Case objLatestEvent.Path_.Class
>
> Case "__InstanceCreationEvent"
> WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName
> _
> & " was created" & vbCrlf
>
> Case "__InstanceDeletionEvent"
> WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName
> _
> & " was deleted" & vbCrlf
>
> Case "__InstanceModificationEvent"
> If objLatestEvent.TargetInstance.LastModified <> _
> objLatestEvent.PreviousInstance.LastModified then
> WScript.Echo Now & vbTab &
> objLatestEvent.TargetInstance.FileName _
> & " was modified" & vbCrlf
> End If
>
> IF objLatestEvent.TargetInstance.LastAccessed <> _
> objLatestEvent.PreviousInstance.LastAccessed then
> WScript.Echo Now & vbTab &
> objLatestEvent.TargetInstance.FileName _
> & " was accessed" & vbCrlf
> End If
>
> End Select
> Loop
>
> Set objWMIService = nothing
> Set colMonitoredEvents = nothing
> Set objLatestEvent = nothing
>
>


You subscribed only to file modification events. The notification query
should start like this:

Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE " ...


with __InstanceOperationEvent instead of __InstanceModiicationEvent

--
urkec
Reply With Quote
  #3  
Old 02-08-2007
Highlander
 
Posts: n/a
Re: Monitor folder for file modification, creation or deletion using W

On Aug 1, 2:38 pm, urkec <ur...@discussions.microsoft.com> wrote:
> "Highlander" wrote:
> > Hello all.

>
> > I've got a VBScript that will monitor a folder for any file
> > modification, creation or deletion. The script detects file
> > modifications just fine; but it does NOT detect any file creation or
> > deletion.

>
> > Any suggestions why this is occurring would be greatly appreciated.
> > Thanks!

>
> > - Dave

>
> > sPath = "\\Server\Drive$\Folder1\Folder2\Folder3"
> > sComputer = split(sPath,"\")(2)
> > sDrive = split(sPath,"\")(3)
> > sDrive = REPLACE(sDrive, "$", ":")
> > sFolders = split(sPath,"$")(1)
> > sFolders = REPLACE(sFolders, "\", "\\") & "\\"

>
> > Set objWMIService = GetObject("winmgmts:\\" & sComputer & "\root
> > \cimv2")
> > Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
> > ("SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE " _
> > & "TargetInstance ISA 'CIM_DataFile' AND " _
> > & "TargetInstance.Drive='" & sDrive & "' AND " _
> > & "TargetInstance.Path='" & sFolders & "'")

>
> > Wscript.Echo vbCrlf & Now & vbTab & _
> > "Begin Monitoring for a Folder Change Event..." & vbCrlf

>
> > Do
> > Set objLatestEvent = colMonitoredEvents.NextEvent
> > Select Case objLatestEvent.Path_.Class

>
> > Case "__InstanceCreationEvent"
> > WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName
> > _
> > & " was created" & vbCrlf

>
> > Case "__InstanceDeletionEvent"
> > WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName
> > _
> > & " was deleted" & vbCrlf

>
> > Case "__InstanceModificationEvent"
> > If objLatestEvent.TargetInstance.LastModified <> _
> > objLatestEvent.PreviousInstance.LastModified then
> > WScript.Echo Now & vbTab &
> > objLatestEvent.TargetInstance.FileName _
> > & " was modified" & vbCrlf
> > End If

>
> > IF objLatestEvent.TargetInstance.LastAccessed <> _
> > objLatestEvent.PreviousInstance.LastAccessed then
> > WScript.Echo Now & vbTab &
> > objLatestEvent.TargetInstance.FileName _
> > & " was accessed" & vbCrlf
> > End If

>
> > End Select
> > Loop

>
> > Set objWMIService = nothing
> > Set colMonitoredEvents = nothing
> > Set objLatestEvent = nothing

>
> You subscribed only to file modification events. The notification query
> should start like this:
>
> Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
> ("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE " ...
>
> with __InstanceOperationEvent instead of __InstanceModiicationEvent
>
> --
> urkec


Duh! Okay I feel stoopid.

Thanks for the correction. Workd like a champ now.

Reply With Quote
  #4  
Old 30-10-2007
martin
 
Posts: n/a
Re: Monitor folder for file modification, creation or deletion usi

Great script. How would you add an email notification to something like this?

"Highlander" wrote:

> On Aug 1, 2:38 pm, urkec <ur...@discussions.microsoft.com> wrote:
> > "Highlander" wrote:
> > > Hello all.

> >
> > > I've got a VBScript that will monitor a folder for any file
> > > modification, creation or deletion. The script detects file
> > > modifications just fine; but it does NOT detect any file creation or
> > > deletion.

> >
> > > Any suggestions why this is occurring would be greatly appreciated.
> > > Thanks!

> >
> > > - Dave

> >
> > > sPath = "\\Server\Drive$\Folder1\Folder2\Folder3"
> > > sComputer = split(sPath,"\")(2)
> > > sDrive = split(sPath,"\")(3)
> > > sDrive = REPLACE(sDrive, "$", ":")
> > > sFolders = split(sPath,"$")(1)
> > > sFolders = REPLACE(sFolders, "\", "\\") & "\\"

> >
> > > Set objWMIService = GetObject("winmgmts:\\" & sComputer & "\root
> > > \cimv2")
> > > Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
> > > ("SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE " _
> > > & "TargetInstance ISA 'CIM_DataFile' AND " _
> > > & "TargetInstance.Drive='" & sDrive & "' AND " _
> > > & "TargetInstance.Path='" & sFolders & "'")

> >
> > > Wscript.Echo vbCrlf & Now & vbTab & _
> > > "Begin Monitoring for a Folder Change Event..." & vbCrlf

> >
> > > Do
> > > Set objLatestEvent = colMonitoredEvents.NextEvent
> > > Select Case objLatestEvent.Path_.Class

> >
> > > Case "__InstanceCreationEvent"
> > > WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName
> > > _
> > > & " was created" & vbCrlf

> >
> > > Case "__InstanceDeletionEvent"
> > > WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName
> > > _
> > > & " was deleted" & vbCrlf

> >
> > > Case "__InstanceModificationEvent"
> > > If objLatestEvent.TargetInstance.LastModified <> _
> > > objLatestEvent.PreviousInstance.LastModified then
> > > WScript.Echo Now & vbTab &
> > > objLatestEvent.TargetInstance.FileName _
> > > & " was modified" & vbCrlf
> > > End If

> >
> > > IF objLatestEvent.TargetInstance.LastAccessed <> _
> > > objLatestEvent.PreviousInstance.LastAccessed then
> > > WScript.Echo Now & vbTab &
> > > objLatestEvent.TargetInstance.FileName _
> > > & " was accessed" & vbCrlf
> > > End If

> >
> > > End Select
> > > Loop

> >
> > > Set objWMIService = nothing
> > > Set colMonitoredEvents = nothing
> > > Set objLatestEvent = nothing

> >
> > You subscribed only to file modification events. The notification query
> > should start like this:
> >
> > Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
> > ("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE " ...
> >
> > with __InstanceOperationEvent instead of __InstanceModiicationEvent
> >
> > --
> > urkec

>
> Duh! Okay I feel stoopid.
>
> Thanks for the correction. Workd like a champ now.
>
>

Reply With Quote
  #5  
Old 18-11-2007
baka
 
Posts: n/a
Re: Monitor folder for file modification, creation or deletion usi

Hello Guys/Gals,

Short and sweet script!.Great people,
i have 2 doubts.
1.how can i monitor subfolder/s also
2.Is there any way to monitor Web folders
such oracle e-files (internet file system) folder etc
Thanks in advance
--Baka
Tokyo Japan


"martin" wrote:

> Great script. How would you add an email notification to something like this?
>
> "Highlander" wrote:
>
> > On Aug 1, 2:38 pm, urkec <ur...@discussions.microsoft.com> wrote:
> > > "Highlander" wrote:
> > > > Hello all.
> > >
> > > > I've got a VBScript that will monitor a folder for any file
> > > > modification, creation or deletion. The script detects file
> > > > modifications just fine; but it does NOT detect any file creation or
> > > > deletion.
> > >
> > > > Any suggestions why this is occurring would be greatly appreciated.
> > > > Thanks!
> > >
> > > > - Dave
> > >
> > > > sPath = "\\Server\Drive$\Folder1\Folder2\Folder3"
> > > > sComputer = split(sPath,"\")(2)
> > > > sDrive = split(sPath,"\")(3)
> > > > sDrive = REPLACE(sDrive, "$", ":")
> > > > sFolders = split(sPath,"$")(1)
> > > > sFolders = REPLACE(sFolders, "\", "\\") & "\\"
> > >
> > > > Set objWMIService = GetObject("winmgmts:\\" & sComputer & "\root
> > > > \cimv2")
> > > > Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
> > > > ("SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE " _
> > > > & "TargetInstance ISA 'CIM_DataFile' AND " _
> > > > & "TargetInstance.Drive='" & sDrive & "' AND " _
> > > > & "TargetInstance.Path='" & sFolders & "'")
> > >
> > > > Wscript.Echo vbCrlf & Now & vbTab & _
> > > > "Begin Monitoring for a Folder Change Event..." & vbCrlf
> > >
> > > > Do
> > > > Set objLatestEvent = colMonitoredEvents.NextEvent
> > > > Select Case objLatestEvent.Path_.Class
> > >
> > > > Case "__InstanceCreationEvent"
> > > > WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName
> > > > _
> > > > & " was created" & vbCrlf
> > >
> > > > Case "__InstanceDeletionEvent"
> > > > WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName
> > > > _
> > > > & " was deleted" & vbCrlf
> > >
> > > > Case "__InstanceModificationEvent"
> > > > If objLatestEvent.TargetInstance.LastModified <> _
> > > > objLatestEvent.PreviousInstance.LastModified then
> > > > WScript.Echo Now & vbTab &
> > > > objLatestEvent.TargetInstance.FileName _
> > > > & " was modified" & vbCrlf
> > > > End If
> > >
> > > > IF objLatestEvent.TargetInstance.LastAccessed <> _
> > > > objLatestEvent.PreviousInstance.LastAccessed then
> > > > WScript.Echo Now & vbTab &
> > > > objLatestEvent.TargetInstance.FileName _
> > > > & " was accessed" & vbCrlf
> > > > End If
> > >
> > > > End Select
> > > > Loop
> > >
> > > > Set objWMIService = nothing
> > > > Set colMonitoredEvents = nothing
> > > > Set objLatestEvent = nothing
> > >
> > > You subscribed only to file modification events. The notification query
> > > should start like this:
> > >
> > > Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
> > > ("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE " ...
> > >
> > > with __InstanceOperationEvent instead of __InstanceModiicationEvent
> > >
> > > --
> > > urkec

> >
> > Duh! Okay I feel stoopid.
> >
> > Thanks for the correction. Workd like a champ now.
> >
> >

Reply With Quote
  #6  
Old 27-01-2009
Member
 
Join Date: Jan 2009
Posts: 1
Re: Monitor folder for file modification, creation or deletion usi

Hi Guys,
I need some help since I am new to WMI Events.
I am trying to write WQL query for monitoring any changes that occured in a file
that is placed in specific folder(C:\Data)
I come up with the following query,but WMIEvent never occures.
Please can you provide me any feedback what I do wrong or if you know othe way to query for file changes that we'll be great as well :)

Here is my WQL query:
SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE TargetInstance ISA "CIM_DataFile" AND TargetInstance.Drive="C:" AND TargetInstance.Path="\\Data"



Thanks in advance
Reply With Quote
Reply

  TechArena Community > Technical Support > Computer Help > Windows Server > Windows Server Help
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Monitor folder for file modification, creation or deletion using WMI"
Thread Thread Starter Forum Replies Last Post
Modify file or folder creation date Abode Portable Devices 4 24-09-2010 07:35 AM
How to get a file last modification date in java? MAGAR Software Development 4 04-02-2010 08:34 PM
Create new folder and move files based on creation date dstiff Windows Server Help 3 13-11-2009 04:04 AM
Problem of user creation or folder sharing KALYAN23 Operating Systems 3 16-10-2009 10:29 PM
Home folder creation via script CHRITOPHER Active Directory 3 15-12-2008 09:39 PM


All times are GMT +5.5. The time now is 07:22 PM.