Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBScript to monitor directory structure changes

Posted on 2009-04-09
10
Medium Priority
?
1,455 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Concerto Cloud Services, a provider of fully managed private, public and hybrid cloud solutions, announced today it was named to the 20 Coolest Cloud Infrastructure Vendors Of The 2017 Cloud  (http://www.concertocloud.com/about/in-the-news/2017/02/0…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
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 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…

688 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