Solved

VBScript to monitor directory structure changes

Posted on 2009-04-09
10
1,419 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 30

Accepted Solution

by:
Scott Helmers earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks a lot, Scott
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Show hidden user account 7 22
Need help editing script 3 49
How to check the PVU´s on AIX TSM servers? 3 46
Exchange, outlook 2 41
More or less everybody in the IT market understands the basics of Networking, however when we start talking about Storage Networks, things get a bit dizzier, and this is where I would like to help.
Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
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 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…

762 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

6 Experts available now in Live!

Get 1:1 Help Now