Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript File Changer Notification 1

Status
Not open for further replies.

Nu2Java

Technical User
Jun 5, 2012
166
US
Hello All

I am quite NEW to VBScript and I am trying to find a script that will notify me by a simple message box that a file has changed/modified/saved in a particular directory. I have several text files in a directory and want to know as soon as one is changed and saved. I have played around with a test program that works in Visual Studio 2010 but for some reason it fails when installed on another PC.

So I have turned to VBScript..... I found some code that will notify me if there are new files added or the count has reached certain limit and scans for changes every x seconds or minutes. What I need is for the script to detect a change as soon as it happens and then notify me.

Any help is GREATLY appreciated. Thanks
 
Constantly monitoring a directory for file changes via a vbscript will consume too many clock cycles and will likely bog down the computer. However, if you run the script as a service or compromise on the frequency at which the folder is monitored, say every 5 seconds, then the job is more feasible with vbs.

Break down the problem into smaller parts.
a) Check for changed files. This can easily be accomplish by acknowledging that the ANY file change will have the most recent modified timestamp. Simply traverse a folders file and look for the most recent modified date. Then compare that date with the previous obtained modified date. If it's more recent, there has been a change.

b) Check for changes in folder content. This is a little more intensive but still fairly easy. All you need to do is compare a list of files generated at point-in-time A with a list of files generated at point-in-time B. The differences will be the files that were either added or removed.

Are you familiar with vbs enough to figure out the code? The following link may help:

-Geates


"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Or you could let WMI do the heavy lifting, and investigate the ExecNotificationQueryAsync method to create an event sink that fires when a file event occurs in a folder
 
I have some code here that I am trying to get working for detecting a file event change... problem is it does not seem to work. Any ideas? Anyone have better code than this?

Thanks,

Code:
strComputer = "."
strMonitoredFolder = "c:\\\\temp\\\\test"  

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process where name = 'wscript.exe' or name = 'cscript.exe'")
 

For Each objProcess In colProcess
   i = i + 1
   Next 
   
   If i > 1 Then    
   '  --- a wscript or cscript process is already running    
   wscript.quit  
   
   
   End If  
   
   strComputer = "."
   Set objWMIService = GetObject("winmgmts:" _  
     & "{impersonationLevel=impersonate}!\\" & _      
       strComputer & "\root\cimv2")
   Set colMonitoredEvents = objWMIService.ExecNotificationQuery _  
     ("SELECT * FROM __InstanceCreationEvent WITHIN 3 WHERE " _   
         & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _     
                & "TargetInstance.GroupComponent= " _        
                        & "'Win32_Directory.Name=""& strMonitoredFolder &""'")
                
i = 1

do
 
If i = 1 Then

	Set objLatestEvent = colMonitoredEvents.NextEvent
        strNewFileName=objLatestEvent.TargetInstance.PartComponent
        Wscript.Echo strNewFileName
'         //here you can write the code for sending out email
    End If
    i = i + 1
loop
 
Someone?? Anyone out there that can help me with this? Very much appreciated!
 
Code:
[blue]strComputer = "."
strDrive = "C:"
strFolder = "\\temp\\test\\"

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    
Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process where name = 'wscript.exe' or name = 'cscript.exe'")

If colProcess.Count > 1 Then
	'  --- a wscript or cscript process is already running
	WScript.quit
End If

' Create the event sink object that receives the events
Set objSink = wscript.CreateObject("WbemScripting.SWbemSink", "SINK_")

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
objWMIService.ExecNotificationQueryAsync objSink, "SELECT * FROM __InstanceCreationEvent WITHIN 3 " & _
"Where Targetinstance Isa 'CIM_DataFile' And " & _
"TargetInstance.Drive = '" & strDrive & "' And " & _
"TargetInstance.Path = '" & strFolder & "'"

Done = False

Do Until Done ' We'll end after first event for this example
	wscript.sleep 1000
Loop

Msgbox "All Done"

Sub SINK_OnObjectReady(wmiObject, wmiAsyncContext)
	wscript.echo wmiObject.TargetInstance.Name
	Done = True
End Sub[/blue]
 
Thanks strongm ... this is just what I am looking for. Is there a way I can make it continuously monitor the folder?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top