VBScript to monitor folders and files creation/deletion

Hi Experts:

I am maintaining a shared folder wherein, we store important data.  Therefore, we have to monitor both folders and files within that drive.  It will be great if I just get a message whenever a folder/file is created/deleted.

Thanks,
LVL 1
adamssapAsked:
Who is Participating?
 
BigBadWolf_000Connect With a Mentor Commented:
See code below....
sPath = "\\Server\Drive$\Folder1\Folder2\Folder3"
sComputer = split(sPath,"\")(2)
sDrive = split(sPath,"\")(3)
sDrive = REPLACE(sDrive, "$", ":")
sFolders = split(sPath,"$")(1)
sFolders = REPLACE(sFolders, "\", "\\") & "\\"
 
Set objWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE " _
& "TargetInstance ISA 'CIM_DataFile' AND " _
& "TargetInstance.Drive='" & sDrive & "' AND " _
& "TargetInstance.Path='" & sFolders & "'")
 
Wscript.Echo vbCrlf & Now & vbTab & _
"Begin Monitoring for a Folder Change Event..." & vbCrlf
 
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
Select Case objLatestEvent.Path_.Class
 
Case "__InstanceCreationEvent"
WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName & " was created" & vbCrlf
 
Case "__InstanceDeletionEvent"
WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName & " was deleted" & vbCrlf
 
Case "__InstanceModificationEvent"
If objLatestEvent.TargetInstance.LastModified <> _
objLatestEvent.PreviousInstance.LastModified then
WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName & " was modified" & vbCrlf
End If
 
IF objLatestEvent.TargetInstance.LastAccessed <> _
objLatestEvent.PreviousInstance.LastAccessed then
WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName & " was accessed" & vbCrlf
End If
 
End Select
Loop
 
Set objWMIService = nothing
Set colMonitoredEvents = nothing
Set objLatestEvent = nothing

Open in new window

0
 
adamssapAuthor Commented:
Hi:

Thank you for the script.  Can I use:
sPath = "\\down025\GRPShare\SAP\ABAP" as the path, instead of $ anywhere.
0
 
BigBadWolf_000Commented:
I only tested on local machine....
So it would work if you ran it on your server "down025"
Say the share is on drive D:
then...
sPath = \\down025\D$\GRPShare\SAP\ABAP

If you want to run it on another PC. Then first map the path to a drive pointer and the modify sPath accordingly. Although I have not tested if this would work.

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
BigBadWolf_000Commented:
sPath = "\\down025\D$\GRPShare\SAP\ABAP"
Oppps forgot quotes
0
 
adamssapAuthor Commented:
Here is the error I am getting.  I done't know where I am going wrong.

Thanks.
sPath = "\\newServer\newDrive$\newFolder"
sComputer = split(sPath,"\")(2)
sDrive = split(sPath,"\")(3)
sDrive = REPLACE(sDrive, "$", ":")
sFolders = split(sPath,"$")(1)
sFolders = REPLACE(sFolders, "\", "\\") & "\\"
 
Set objWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")

Open in new window

error.JPG
0
 
adamssapAuthor Commented:
Script works great on local machine, but I am getting the above error, if I am trying to monitor on the shared folder.
0
 
BigBadWolf_000Commented:
sPath = "\\newServer\newDrive$\newFolder"

newServer = is the computer name
newDrive$ =  is the drive letter as in D$ or C$ or Z$
newFolder = is the actual folder name - not the share name

You logged in as admin or equal?

I have only a WinXP OS to test with today...I created share and tested
sPath = "\\voyager\D$\X"
Where voyager is computer name
D$ = D drive on which the shared folder resides
X = the shared folder name (actual share name is Xest)
Hope that clears it up
0
 
adamssapAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.