Link to home
Start Free TrialLog in
Avatar of adamssap
adamssapFlag for Afghanistan

asked on

VBScript to monitor directory structure changes

Hi Experts:
We have a shared drive wherein we have a very large directory structure.
Is there a VBScript to monitor directory structure changes?  I want to run the script everyday to see if the Directory structure has changed or not.  
Avatar of Scott Helmers
Scott Helmers
Flag of United States of America image

There a number of good examples at the Hey, Scripting Guy site at Microsoft TechNet that could be adapted to do what you want.

A couple that look interesting

Avatar of adamssap

ASKER

Hi Scott:
Thanks for your reply.  
I used the code below from the links you sent me.  It works fine when someone creates a new folder in the directory, but it does nothing when someone deletes a folder.



strComputer = "."
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
        & "TargetInstance ISA 'Win32_SubDirectory' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=""C:\\\\Scripts""'")
 
Do While True
    Set objEventObject = colMonitoredEvents.NextEvent()
    Wscript.Echo objEventObject.TargetInstance.PartComponent
Loop
 
 
 
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
        & "TargetInstance ISA 'Win32_Subdirectory' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=""C:\\\\Scripts""'")
Set objEventObject = colMonitoredEvents.NextEvent()
 
 
Do While True
    Set objEventObject = colMonitoredEvents.NextEvent()
    strNewFolder = objEventObject.TargetInstance.PartComponent
    arrNewFolder = Split(strNewFolder, "=")
    strNewFolder = arrNewFolder(1)
    strNewFolder = Replace(strNewFolder, "\\", "\")
    strNewFolder = Replace(strNewFolder, Chr(34), "")
    Wscript.Echo strNewFolder
Loop
 
SWbemServicesEx: Quota violation

Open in new window

One thing that is definitely wrong is that __InstanceCreationEvent needs to be changed to __InstanceDeletionEvent. However, even when I did that I couldn't get the deletion event to fire either... I also found four or script variations on this theme and couldn't get them to work either, though all were from reputable sources. There must be a secret sauce that I'm missing.

Back to your original question, I had a different impression of what you wanted. I thought the second link I provided above might be more useful, i.e., building an array of all directories as the baseline for future comparisons. You could store the array data in a text file or spreadsheet, then once a day you could compare your new list with the old list. This would definitely highlight both additions and deletions (though the compare logic could get a bit tricky if you expect a lot of changes).  Nothing wrong with real-time monitoring but if you want a once-a-day list, the approach would be a bit different.
adamssap -- let me know whether my presumption in the previous post is correct that you really want a "baseline" list against which to compare every day, i.e., that you don't really want to monitor folder activity in real time. If so, I think I've come up with a reasonably easy way to accomplish this... not fully developed yet but the basic ideas works.
Hi Scott:

Thanks for your time and reply.  I am basically looking for real-time monitoring the folder activity.  I am sorry for not being specific/clear in my original question.
ASKER CERTIFIED SOLUTION
Avatar of Scott Helmers
Scott Helmers
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Scott:

Hats off to your good work.  Folder creation and file monitoring scripts working perfectly fine.  I will wait for Folder deletion part from you and close this question.  I really appreciate your help.

Thanks and God bless you.

Well, I still can't find a way to monitor folder deletion directly. I think your best bet is to use the pair of scripts I posted on 4/12/2009 -- run the first to create a file containing your baseline directory structure, and use the Windows scheduler to run the second one as often as you need an update.

I think that's the best I can do!

Scott
Thanks a lot, Scott