2

I need to create a batch script that continually monitors a specific file for changes, in this case, LogTest.txt.

When the file is updated it will trigger a VBScript message box, which displays the last line from within LogTest.txt. The idea is that this will continue monitoring after the message box is cleared.

I have tried using the forfiles option, but this really only lets me deal with the date and not the time. I know that PowerShell and other options are available, but for reasons that are just too long to explain I am limited to being only able to use a batch and VBScript.

Batch File:

@echo off

:RENEW

forfiles /m LogTest.txt /d 0
if %errorlevel% == 0 (
  echo The file was modified today
  forfiles /M LogTest.txt /C "cmd /c echo @file @fdate @ftime"
  cscript MessageBox.vbs "Error found."
  REM do whatever else you need to do
) else (
  echo The file has not been modified today
  dir /T:W LogTest.txt
  REM do whatever else you need to do
)

goto :RENEW     

MessageBox.vbs:

Set objArgs = WScript.Arguments
messageText = objArgs(0)
MsgBox "This is an error", vbOkCancel + vbExclamation, "Error Found"
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Jamie Mry
  • 23
  • 1
  • 4
  • 1
    Possible duplicate of [batch file to monitor additions to download folder](http://stackoverflow.com/questions/4230976/batch-file-to-monitor-additions-to-download-folder) – JabberwockyDecompiler Apr 30 '16 at 05:26
  • Thanks for the comment, its not for additional files to a directory, it to only monitor one particular file for and write updates. – Jamie Mry Apr 30 '16 at 05:36
  • If you're going to use VBScript anyway I'd recommend doing the monitoring [entirely in VBScript](http://stackoverflow.com/a/16119815/1630171). – Ansgar Wiechers May 01 '16 at 11:48

1 Answers1

6

There is an archive attribute on every file. Windows sets this attribute on every write access to the file.

You can set it with the attrib command and check it for example with:

@echo off
:loop  
timeout -t 1 >nul  
for %%i in (LogTest.txt) do echo %%~ai|find "a">nul || goto :loop
echo file was changed
rem do workload
attrib -a LogTest.txt
goto :loop

timeout /t 1 >nul: small wait interval to reduce CPU-Load (never build a loop without some idle time)

for %%i in (logTest.txt) do... process the file

echo %%~ai print the attributes (see for /? for details)

|find "a" >nul try to find the "a"rchive-attribute in the output of the previous echo and redirect any output to nirvana (we don't need it, just the errorlevel)

|| goto :loop works as "if previous command (find) failed, then start again at the label :loop"

If find was successful (there is the archive attribute), then the next lines will be processed (echo file was changed...)

attrib -a LogTest.txt unsets the archive attribute of the file.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thanks for the information, I have been trying to determine how I can use this, but for a beginner like me I'm struggling to utilize this in a script. – Jamie Mry Apr 30 '16 at 08:45
  • just put it in a new batchfile and run it. Then edit `logtest.txt` (while the batch is running) and see what happens. – Stephan Apr 30 '16 at 08:51
  • Stephan your a genius! I just added a call line for my MessageBox VB script after the "echo file was changed" and it seems to be working well, I will test this tom and see if it will monitor a file on a network share. Thanks for your help. – Jamie Mry Apr 30 '16 at 10:15
  • yes, it does: `for %i in (\\192.168.178.55\transfer\test.txt) do @echo %i` (`%%i` when using it in a batch file, of course) – Stephan Apr 30 '16 at 10:24
  • 1
    @JamieMry On StackOverflow you should mark the answer that helped you as accepted. To do this click the green check box by the answer. This shows other people this has the answer you were looking for as well as others that may have a similar issue. – JabberwockyDecompiler May 01 '16 at 01:25
  • Hi Stephan, thanks again for all your help, the batch file is working great. My only issue now is that I don't fully understand the logic, is there any chance you can break it down for me so that I can learn from this. Cheers again – Jamie Mry May 01 '16 at 22:22
  • when i run this script, file was changed is console logged even if i dont make any change in the file, why is this happening? – Achal Urankar Feb 18 '23 at 11:11
  • 1
    @AchalUrankar: I can only guess. Maybe better ask a new question (referencing this one for context) and include your exact output (if it doesn't seem helpful, remove `@echo off`) – Stephan Feb 18 '23 at 13:02