Solved

VBScript to monitor directory structure changes

Posted on 2009-04-09
10
1,426 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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 …
The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
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…
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…

808 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