Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - More Event Scripting with WMI
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

More Event Scripting with WMI
By: Nilpo/Developer Shed Staff Writer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-05-15

    Table of Contents:
  • More Event Scripting with WMI
  • Using __InstanceModificationEvent
  • Parsing WMI’s results
  • Using __InstanceOperationEvent

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Windows Scripting Articles
    More By Nilpo/Developer Shed Staff Writer


       · Learn what other events are made available by WMI.
     

    WINDOWS SCRIPTING ARTICLES

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT