Solved

VBScript to monitor directory structure changes

Posted on 2009-04-09
10
1,443 Views
Last Modified: 2013-11-14
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.  
0
Comment
Question by:adamssap
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 31

Expert Comment

by:Scott Helmers
ID: 24112102
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

0
 
LVL 1

Author Comment

by:adamssap
ID: 24117297
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

0
 
LVL 31

Expert Comment

by:Scott Helmers
ID: 24118639
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Expert Comment

by:Scott Helmers
ID: 24120850
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.
0
 
LVL 1

Author Comment

by:adamssap
ID: 24126856
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.
0
 
LVL 31

Accepted Solution

by:
Scott Helmers earned 500 total points
ID: 24127195
Well -- the attached may not be what you want, but I got intrigued by making this work so here it is anyway :-)  {it gave me a chance to play with the dictionary object for the first time]

There are two scripts attached. One creates a text file containing the "master" list of subfolders in any folder you select. Run it whenever you want to establish the baseline.

Run the second one anytime you want to compare the current set of subfolders with the baseline.

At the moment, the output is merely a list sent to a message box; clearly, it could be written to a text file, an Excel spreadsheet, etc.

I'll try to get back to the real-time monitoring of deleted folders later this week. The good news is that at least you can monitor additions!

BTW, EE doesn't allow upload of vbs files so change the extensions on the files below from txt to vbs

Create-baseline-folder-list.txt
Compare-current-to-baseline.txt
0
 
LVL 31

Assisted Solution

by:Scott Helmers
Scott Helmers earned 500 total points
ID: 24127568
No matter what I try I can monitor correctly for FOLDER creation but only monitor for FILE deletion. I have no idea why but that seems to be the consensus of every scripting site I can find. (Copies of working scripts below.)

I did look around and found a variety of shareware products that claim to monitor for folder create and deletion. I haven't tried any of them but here is a sampling; most/all have free downloads with purchase prices that range from $25 to $100.

http://www.sentry-go.com/qm-file-monitoring.aspx
http://www.tucows.com/preview/508651
http://sharealarm.nsauditor.com/help/folder_watcher.html
http://www.jpsoft.com/help/index.htm?foldermonitor.htm

Other than that, you can just schedule the "Compare current to baseline" script I posted earlier to run periodically...

Monitor-folder-creation.txt
Monitor-file-deletion.txt
0
 
LVL 1

Author Comment

by:adamssap
ID: 24130788
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.

0
 
LVL 31

Expert Comment

by:Scott Helmers
ID: 24180749
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
0
 
LVL 1

Author Closing Comment

by:adamssap
ID: 31572304
Thanks a lot, Scott
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The question appears often enough, how do I transfer my data from my old server to the new server while preserving file shares, share permissions, and NTFS permisions.  Here are my tips for handling such a transfer.
Many businesses neglect disaster recovery and treat it as an after-thought. I can tell you first hand that data will be lost, hard drives die, servers will be hacked, and careless (or malicious) employees can ruin your data.
This video teaches viewers how to encrypt an external drive that requires a password to read and edit the drive. All tasks are done in Disk Utility. Plug in the external drive you wish to encrypt: Make sure all previous data on the drive has been …
This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question