Thread: File monitor

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    VBScript File monitor


    Hi,
    I would like to monitor a text file. Currently I use the following vbscript code:

    Code:
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    
    'Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    '    ("SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE " _
    '        & "TargetInstance ISA 'CIM_DataFile' and " _
    '            & "TargetInstance.Name='U:\\Programming\\Hydra\\Label\\LogFile01.txt'")
    
    strPnrPath01 = "U:\\Programming\\Test05\\Test01.txt"
    
    Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
        ("SELECT * FROM __InstanceOperationEvent WITHIN 20 WHERE " _
            & "TargetInstance ISA 'CIM_DataFile' and " _
                & "TargetInstance.Name='" & strPnrPath01 & "'")
    
    
    Do
        Set objLatestEvent = colMonitoredEvents.NextEvent
        'WScript.Echo "File: " & objLatestEvent.TargetInstance.Name
        'WScript.Echo "New size: " & objLatestEvent.TargetInstance.FileSize
        'Wscript.Echo "Old size: " & objLatestEvent.PreviousInstance.FileSize
        
        	Select Case objLatestEvent.Path_.Class
    			'Case "__InstanceCreationEvent"
    			'	WScript.Echo Now & vbTab & MyFile & " was created" & vbCrlf      
    			'Case "__InstanceDeletionEvent"     
    			'	WScript.Echo Now & vbTab & MyFile & " was deleted" & vbCrLf
    			Case "__InstanceModificationEvent"
    				If objLatestEvent.TargetInstance.LastModified <> objLatestEvent.PreviousInstance.LastModified Then
    						'WScript.Echo Now & vbTab & MyFile & " was modified" & vbCrLf
    						MsgBox "File was modified."
    						
    				End If
    		End Select
    
        
    Loop
    
    Set objWMIService = Nothing
    Set colMonitoredEvents = Nothing
    Set objLatestEvent = Nothing
    The text file is modified 5 or 6 times in a minute or two, and I would like to be notified only after the last file modification (the attached code notifies me after the first modification). How could I do that? (The text file modification procedure: the file is being changed many times - about 5 or 6 times - in a minute, after that minute the file is unchanged in the next several hours).
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    44
    The "Timer" function returns the number of seconds since midnight. After the first detection, you would put your script into a loop and check when the desired time has expired. InVB6 I would normally use a DoEvents function within that loop, but I don't believe that it works in VBScript, and I also believe that it is not necessary because VBScript is a Pseudo language. Make sure to account for a change that might occur at midnight.

    J.A. Coutts
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    File monitor


    Which S/W is create with the help of Visual basic then this s/w setting change like size change according to used monitor.

    Comments on this post

    • medialint disagrees : What the .... ?????
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    99
    Rep Power
    157

    why do you monitor file


    what is the size and contents of the file

    it may be just as easy to read it

    here to help

IMN logo majestic logo threadwatch logo seochat tools logo