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

5 pts.
Tags:
Batch files
Microsoft Windows
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:templastbarcode.txt

Answer Wiki

Thanks. We'll let you know when a new response is added.

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

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following