scripting - Monitor Drive. Using VB Script -


i want monitor drive file changes, using vbscript. have below code. works fine instancecreationevent , instancedeletionevent. instancemodificationevent not happening. googling got know need use cim_datafile instead of cim_directorycontainsfile monitor instancemodificationevent. not sure how modify code. can help.

fyi: 1 script should monitor folders , subfolders in drive.

ps: suggestion improve code , performance or other ideas welcome.

my code:

dim arrfolders  dim strcomputer  dim objwmiservice  dim strfolder  dim strcommand  dim  dim strquery   strchangefile = "monitorfolder_log.txt" strmailidfile = "monitorfolder_mailids.txt"  'check if log file exists, if not ceate new file , exit script. restart script again. set ofso = createobject("scripting.filesystemobject")      if not ofso.fileexists(strchangefile)      'wscript.echo "change log file not found. creating new file..."     set otxtfile = ofso.createtextfile(strchangefile)       wscript.echo strchangefile & " file created." & vbcrlf & "please restart script." & vbcrlf     wscript.quit end if  'prompt drive should monitored. if not valid drive, exit script. strdrive = inputbox("enter drive monitor: " & vbcrlf & "e.g.: input c monitor c:\ drive.", "monitor folder - oracle", "e") if strdrive = ""     wscript.echo "not valid drive. terminating script."     wscript.quit end if  'append ":" drive name. strdrive = strdrive & ":"  'read mail ids. set objfsomailid = createobject("scripting.filesystemobject") set otsmailid = objfsomailid.opentextfile(strmailidfile) strmailidslist = otsmailid.readall otsmailid.close 'wscript.echo strmailidslist  'array store existing folder paths should monitored. arrfolders = array() = 0  set fso = createobject("scripting.filesystemobject") showsubfolders fso.getfolder(strdrive)  sub showsubfolders(folder)     each subfolder in folder.subfolders     = + 1         folderpath = "" & subfolder.path & ""     folderpath = replace(folderpath ,"\","\\\\")     redim preserve arrfolders(i)     arrfolders(i) = folderpath     'wscript.echo & " " & arrfolders(i)         showsubfolders subfolder     next end sub   'set first path drive. arrfolders(0) = strdrive & "\\\\"  'use wmi query file changes. strcomputer = "."  set objwmiservice = getobject("winmgmts:\\" & strcomputer & "\root\cimv2")  'loop throught array of folders setting monitor each  = 0  each strfolder in arrfolders     'create event sink     'wscript.echo "setup folder: " & strfolder & vblf    strcommand = "set eventsink" & & " = wscript.createobject" & "(""wbemscripting.swbemsink"", ""sink" & & "_"")"     executeglobal strcommand    'setup notification     strquery = "select * " _            & "from __instanceoperationevent " _            & "within 1 " _            & "where targetinstance isa 'cim_directorycontainsfile'" _            & "  , targetinstance.groupcomponent = " & "'win32_directory.name=""" & strfolder & """'"    strcommand = "objwmiservice.execnotificationqueryasync eventsink" & & ", strquery"    executeglobal strcommand     'create onobjectready sub     strcommand = "sub sink" & & "_onobjectready(objobject, " &  "objasynccontext)" & vblf _              & "  'wscript.echo objobject.targetinstance.partcomponent" & vblf _              & "  sendnotification(objobject)" & vblf _              & "end sub"    'wscript.echo strcommand     executeglobal strcommand     = + 1  next   'wait events. wscript.echo "waiting events..."  = 0  while (true)     wscript.sleep(1000)  wend   function sendnotification(objobject)      streventtype = objobject.path_.class     strpartcomp = split(objobject.targetinstance.partcomponent, "=")     strfilename = replace(strpartcomp(1), "\\", "\")      wscript.echo streventtype     wscript.echo strfilename      'some more code send mail , logs...  end function 

monitoring entire filesystem file creation not feasible. eat system resources , might severly affect system operation. ever monitor selected folders. following should work:

const interval = 1  set monitor = createmonitor("c:\foo")   set evt = monitor.nextevent()   select case evt.path_.class     case "__instancecreationevent"     : sendnotification evt.targetinstance     case "__instancemodificationevent" : ...     case "__instancedeletionevent"     : ...   end select loop  function createmonitor(path)   set wmi = getobject("winmgmts://./root/cimv2")   set fso = createobject("scripting.filesystemobject")    path = split(fso.getabsolutepathname(path), ":")   drv  = path(0) & ":"   dir  = replace(path(1), "\", "\\")   if right(dir, 2) <> "\\" dir = dir & "\\"    query = "select * __instanceoperationevent" & _           " within " & interval & _           " targetinstance isa 'cim_datafile'" & _           " , targetinstance.drive='" & drv & "'" & _           " , targetinstance.path='" & dir & "'"   set createmonitor = wmi.execnotificationquery(query) end function  sub sendnotification(tgtinst)   'send notification end sub 

you should run monitors different folders separate processes, because nextevent() blocking operation.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -