More Event Scripting with WMI - Parsing WMI’s results
(Page 3 of 4 )
As you can see, WMI returns a full WMI path to the object. Since this is returned as a string, we can create a function to parse the data.
Function parsePath(strWMIpath)
strPath = Right(strWMIpath, Len(strWMIpath) - InStr(strWMIpath, "="))
strPath = Left(strPath, Len(strPath) - 1)
strPath = Right(strPath, Len(strPath) - 1)
strPath = Replace(strPath, "", "")
parsePath = strPath
End Function
Okay, there’s lot of things going on here. Let me try to explain what’s happening. If you look at the string path returned by WMI you will see that we only need the information to the right of the equal sign. So the first part returns just that.
We use the InStr function to return the position of the equals sign. By subtracting that number from the total length of the string, we can determine how many characters appear after it. Then we use the Right function to return only that portion of the string.
"c:scriptsDocument.txt"
Now we have our file path. However, it’s enclosed in quotation marks. We probably wouldn’t want that either so we use the next two lines to clip a single character from each side of the string.
c:scriptsDocument.txt
This leaves us with our file path, but there’s still one more problem. Our file path still contains WMI’s escaped backslashes. To eliminate those, we’ll use VBScript’s Replace function to replace all occurrences of a double backslash with a single one.
c:scriptsDocument.txt
Now strPath contains only our formatted file path. We wrap up our function by telling it to return the contents of strPath.
Now to implement this in our code we need to make one minor change inside of our Do…Loop. Rather than echoing back WMI’s path, we need to pass it through our function first.
Do While True
Set objEvent = colEvents.NextEvent()
WScript.Echo "File Modified:", _
parsePath(objEvent.TargetInstance.PartComponent)
Loop
Now our script returns exactly the information we wanted. If we wanted, we could pass this path on to FileSystemObject method, for example, to back up the newly modified file. Or, as in our example, we simply echo it back.
Next: Using __InstanceOperationEvent >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer