Question

  Asked: Mar 9 2008   5:55 AM GMT
  Asked by: Probert


How to monitor a file in windows for changes and run a program when it changes


Windows, Batch files, Scripting

I have a single text file that contains a 5 digit number of the last barcode scanned when a user enters a facility.
How in Windows do I make a batch file that watches for the file to change then execute command?
for example, the file is c:\temp\lastbarcode.txt

Subscribe to Alerts! Get questions and answers delivered to your Inbox.


E-mail me updates on this question



   SUBSCRIBE

hidden modal window

Answer Wiki (Improve, edit or add to this answer)


 RATE THIS ANSWER
0
Click to Vote:
  •   0
  •  0



So a batch file would not be the best way of doing this. This script or app would need to continuously be run to scan for the changes to your text file.

Unless you can add a line of code to whatever makes the modification to execute your batchfile then you would want a VB script or service that will tail the end of your text document on file modification. and that could execute your command or batch file.

I can help you write this if you are more specific with your requirements.

I found the following Script and modified it to produce the results you describe :


strComputer = "."
Set objShell = CreateObject("Wscript.Shell")

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")

Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE " _
& "TargetInstance ISA 'CIM_DataFile' and " _
& "TargetInstance.Name='c:\\temp\\lastbarcode.txt'")

Do
Set objLatestEvent = colMonitoredEvents.NextEvent
' HERE IS Where You would specify the command you wish to execute
' something along the lines of : objShell.Run [Enter Your Application Name Here]
comd="c:\windows\system32\notepad.exe c:\temp\lastbarcode.txt"
objShell.Run comd
WScript.Echo "Now - " & Now()
Loop
  • AddThis Social Bookmark Button

Browse more Questions and Answers on Microsoft Windows.

Looking for relevant Microsoft Windows Whitepapers? Visit the SearchWinIT.com Research Library.


Discuss This Answer


You must be logged-in to discuss a question. Log-in/Register