Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...This has been the MOST helpful forum that I have been a part of and I want to say thank you. The tips, tricks and helpful advice that you all contribute to have been lifesavers in many instances..."

Geography

Where in the world do Tek-Tips members come from?

VBScript File Changer NotificationHelpful Member! 

Nu2Java (TechnicalUser)
6 Jun 12 11:01
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
Geates (Programmer)
6 Jun 12 14:32
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:
http://raqxtr.sstar.com/caspdoc/html/vbscript_language_reference.htm

-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

Helpful Member!  strongm (MIS)
7 Jun 12 12:42
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
Nu2Java (TechnicalUser)
15 Sep 12 19:25
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 
Nu2Java (TechnicalUser)
17 Sep 12 21:50
Someone?? Anyone out there that can help me with this? Very much appreciated!
strongm (MIS)
18 Sep 12 6:40

CODE

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 
Nu2Java (TechnicalUser)
18 Sep 12 9:35
Thanks strongm ... this is just what I am looking for. Is there a way I can make it continuously monitor the folder?
strongm (MIS)
18 Sep 12 9:59
Change

Do Until Done

to

Do While True

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close