• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2329
  • Last Modified:

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,
0
adamssap
Asked:
adamssap
  • 4
  • 4
1 Solution
 
BigBadWolf_000Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

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.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now