0

I'm trying to write some code (in batch/powershell) that will launch a batch script that I've made (with the help of some great people here) when a change is detected in a logfile, I've tried some solutions, such as this one, but it wouldn't correctly detect when the file was modified, which I believe to be due it being open in the program that is logging to it. I found this

Get-content filename -Tail 0 -Wait

powershell command that would correctly display any new changes added to the file, but I'm not sure if I can use that to launch my other script. Here is that script if it helps

@echo off
copy foobar.log file.log /Y > NUL
echo Loading...>currentsong.txt
timeout /t 5 /nobreak > NUL
setlocal enabledelayedexpansion
for /f tokens^=2^ delims^=^" %%a in (file.log) do (
  set "lastLine=%%~na"
  )
echo %lastLine%>currentsong.txt
endlocal

Any help is greatly appreciated. Thanks!

  • I bet you are monitoring foobar.log. It is possible foobar does not close the handle on foobar.log while it open, and as such the change is not registered until foobar is closed. Can you see if that is the case? – LPChip Apr 23 '20 at 20:41
  • Yes, I am trying to monitor foobar.log for changes, the change is registered, even when foobar is open, yet the watcher doesn't seem to pick it up, I was thinking about getting the modified time putting it into a variable and then comparing it to the current modified time (after which I launch the script and reset the variable to the current time and loop the program), since the modified time will change every time the log is updated. I'm not really sure how to go about doing that, though. – user1167553 Apr 23 '20 at 20:48
  • So, you have powershell open, run the script, foobar has a change, you close foobar, yet powershell still does not return a file change? Can you even make powershell print something to its console? – LPChip Apr 23 '20 at 21:07
  • The way watcher works is by outputting everything that happened while it was running in a log file, none of the modifications are in the log file, even after closing foobar. – user1167553 Apr 23 '20 at 21:19
  • Ah, so you are trying to make changes to the log file while foobar is running? I bet foobar keeps the logfile in memory, and appends to it in memory, and write what it has in memory directly to the file (or something similar) so it basically is undoing your changes. What happens if you perform the action manually? Eg. see that the file changed, open it with notepad, make the change, save and close and reopen after another change was made? – LPChip Apr 23 '20 at 21:21
  • It won't allow me to manually change a file while foobar has it open, however when I close it, it will record the manual modifications. – user1167553 Apr 23 '20 at 21:31
  • So... you can't alter it yourself, how can your script then? This is basically the answer. It does not work because it is not possible to alter the file while foobar is running. Your changes are not reverted back, there are simply no changes. – LPChip Apr 24 '20 at 06:27

1 Answers1

0

PowerShell:

gc -Path .\newlog.txt -tail 0 -wait | ForEach{
    # Extract file BaseName
    $_.split('"')[1].split('\')[-1].split('.')[0] 
    # Any other code you want to execute when log is updated.
}

This will loop waiting for file updates even after the file is closed. You'll have to code a breakout test or use Ctrl+C.

Keith Miller
  • 8,704
  • 1
  • 15
  • 28