Modify VBScript

davidam

Active Member
Joined
May 28, 2010
Messages
497
Office Version
  1. 2021
Platform
  1. Windows
I quote below some code from Mr Ivan Moala. This code loops out of the script when an event--a file found, deleted or modified in a directory-- becomes true. Would someone be able to show me how to make this run continuously? I thought of removing the Do element and having it just go back to the beginning but apparently goto is not part of vbscript. Thanks.

Code:
Dim MyExcel
Set MyExcel = GetObject("C:\ExcelFiles\Useful\MonitorDirectory.xls")
 
''
strComputer = "."
'// Note 4 forward slashes!
strDirToMonitor = "C:\\\\A_BOOK"
'// Monitor Above every 10 secs...
strTime = "10"
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceOperationEvent WITHIN " & strTime & " WHERE " _
        & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=" & Chr(34) & strDirToMonitor & Chr(34) & "'")
 
 
Do While True
    Set objEventObject = colMonitoredEvents.NextEvent()
 
    Select Case objEventObject.Path_.Class
        Case "__InstanceCreationEvent"
            MsgBox "A new file was just created: " & _
                objEventObject.TargetInstance.PartComponent
 
            MyFile = StrReverse(objEventObject.TargetInstance.PartComponent)
            '// Get the string to the left of the first \ and reverse it
            MyFile = (StrReverse(Left(MyFile, InStr(MyFile, "\") - 1)))
            MyFile = Mid(MyFile, 1, Len(MyFile) - 1)
            With MyExcel.Worksheets(1)
                 .Range("A65536").End(-4162).Offset(1, 0).Value = MyFile
            End With
            Exit Do
        Case "__InstanceDeletionEvent"
            MsgBox "A file was just deleted: " & _
                objEventObject.TargetInstance.PartComponent
            MyFile = StrReverse(objEventObject.TargetInstance.PartComponent)
            '// Get the string to the left of the first \ and reverse it
            MyFile = (StrReverse(Left(MyFile, InStr(MyFile, "\") - 1)))
            MyFile = Mid(MyFile, 1, Len(MyFile) - 1)
            With MyExcel.Worksheets(1)
                 .Range("A65536").End(-4162).Offset(1, 0).Value = MyFile
            End With
            Exit Do
        Case "__InstanceModificationEvent"
            MsgBox "A file was just modified: " & _
                objEventObject.TargetInstance.PartComponent
            Exit Do
    End Select
Loop
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Reviving this one...
Is it possible to add to this to check two different locations?
 
Upvote 0
Please start a new thread, linking to this one.
I didn't see this come up as a New Post (which I realized after was because there was an unread only filter applied to it) so I went ahead and created a new thread.
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,121
Members
452,381
Latest member
Nova88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top