[Win32utils-devel] win32ole properties question

Heesob Park phasis at gmail.com
Sat Jan 18 04:16:12 UTC 2014


Hi,

2014/1/18 Daniel Berger <djberg96 at gmail.com>:
>
>
>
> On Fri, Jan 17, 2014 at 8:11 PM, Daniel Berger <djberg96 at gmail.com> wrote:
>>
>>
>>
>>
>> On Fri, Jan 17, 2014 at 7:38 PM, Heesob Park <phasis at gmail.com> wrote:
>>>
>>> Hi,
>>>
>>> 2014/1/18 Daniel Berger <djberg96 at gmail.com>:
>>> > Hi all,
>>> >
>>> > I'm trying to duplicate some code here in the ole branch of
>>> > win32-changejournal:
>>> >
>>> >
>>> > http://www.codeproject.com/Articles/42212/WMI-and-File-System-Monitoring
>>> >
>>> > I'm basically having trouble duplicating this bit:
>>> >
>>> > objPrevInst.Properties_(objProperty.Name)
>>> >
>>> > How do I do that with Ruby?
>>> >
>>> That would be translated as
>>> objPrevInst.send(objProperty.Name)
>>
>>
>> Silly me. I thought I tried that and it didn't work.
>
>
> Hm, no, still can't make it work. It's probably something horribly obvious
> but I'm not seeing it.
>
Here is a whole working translation code:

require 'win32ole'

intInterval = "2"
strDrive = "C:"
strFolder = "\\\\scripts\\\\"
strComputer = "."

# Connect to WMI
objWMIService = WIN32OLE.connect("winmgmts:" +
    "{impersonationLevel=impersonate}!//" +
    strComputer + "\\root\\cimv2")


# The query string

strQuery =  "Select * From __InstanceOperationEvent" +
     " Within " + intInterval +
     " Where Targetinstance Isa 'CIM_DataFile'" +
     " And TargetInstance.Drive='" + strDrive + "'" +
     " And TargetInstance.Path='" + strFolder + "'"

# Execute the query

colEvents = objWMIService.ExecNotificationQuery(strQuery)

# The loop

while true do
    # Wait for the next event
    # Get SWbemEventSource object
    # Get SWbemObject for the target instance

    objEvent = colEvents.NextEvent()
    objTargetInst = objEvent.TargetInstance

    # Check the class name for SWbemEventSource
    # It cane be one of the following:
    # - __InstanceCreationEvent
    # - __INstanceDeletionEvent
    # - __InstanceModificationEvent
    case objEvent.Path_.Class

        # If it is file creation or deletion event
        # just echo the file name

        when "__InstanceCreationEvent"
            puts "Created: " + objTargetInst.Name

        when "__InstanceDeletionEvent"
            puts "Deleted: " + objTargetInst.Name

        # If it is file modification event,
        # compare property values of the target and previous
        # instance and echo the properties that have changed

        when "__InstanceModificationEvent"

            objPrevInst = objEvent.PreviousInstance

            objTargetInst.Properties_.each do |objProperty|
                if objProperty.Value != objPrevInst.send(objProperty.Name)
                    puts "Changed:        " +
                         objTargetInst.Name
                    puts "Property:       " +
                         objProperty.Name
                    puts "Previous value: " +
                         objPrevInst.send(objProperty.Name)
                    puts "New value:      " +
                         objProperty.Value
                    puts
                end
            end
    end

end


Regards,
Park Heesob


More information about the win32utils-devel mailing list