Solved

VBScript to monitor directory structure changes

Posted on 2009-04-09
10
1,423 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
  • 6
  • 4
10 Comments
 
LVL 30

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 30

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
 
LVL 30

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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 30

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 30

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 30

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
AWS Glacier is Amazons cheapest storage option and is their answer to a ‘Cold’ storage service.  Customers primarily use this service for archival purposes and storage of infrastructure backups.  Its unlimited storage potential and low storage cost …
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
This Micro Tutorial will teach you how to reformat your flash drive. Sometimes your flash drive may have issues carrying files so this will completely restore it to manufacturing settings. Make sure to backup all files before reformatting. This w…

947 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now