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!

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
  • 10,694
  • 1
  • 20
  • 35