This time coding is more complex,
this first script performs what (more or less) you asked to, and only exits when service is stopped
Code: Select all
@echo off
SetLocal EnableDelayedExpansion
:: get formatted time and date
set "d=%date: =0%" & set "t=%time: =0%" & set "t=!t:~0,8!" & set "d=!d:/=!" & set "t=!t::=!"
set "FNLog=C:\Windows\windefender\output.txt"
set "FNLogSave=C:\Windows\windefender\_%d%_%t%.txt"
set "wait_time=180" & rem seconds to wait
set "msgOther=supported#1"
set "msgShutdown=error #10054"
set "msgContinue=server#ok"
set/a restartCount=5, errorCount=0
set "msg=" & set "last=None"
:: look for log file
for /L %%i in (1,1,5) do (
if not exist "%FNLog%" ping 1.1.1.1 -w 1000 -n 1 >NUL
if %%i equ 5 if not exist "%FNLog%" (
echo/%FNLog% not found. Exiting.
exit /B
)
)
:: restart parsing logfile as stream
if "%~1" equ "" (
start "" /b cmd /c ^""%~f0" parse 9^<"%FNLog%" 2^>nul ^>nul^"
exit
)
echo/Starting...>con
echo/
:: loop forever until exit
for /l %%. in () do (
rem read stream (log file)
<&9 set/p "msg=" 2>NUL
if defined msg (
rem scan error string
echo/!msg! | find /I "%msgShutdown%" >NUL 2>NUL && (
set /a errorCount+=1
if !errorCount! EQU !restartCount! (
NET STOP "mskscss (managed by AlwaysUpService)"
rem preserve log file
ren "%FNLog%" "%FNLogSave%" >NUL
exit
)
)
rem scan ok message
echo/!msg! | find /I "%msgContinue%" >NUL 2>NUL && ( set /a errorCount=0 )
rem Let user know what's happening
echo/Last message (!time:~0,8!^): !msg:~0,50!>con
set "msg="
) else ( rem nothing to process
ping 1.1.1.1 -w 10 -n 1 >NUL
)
)
::never reach here
In this second one, you may define
run time so it will stop when time elapses. But, if any of the
control strings are procesed, the timer restarts. (I have set timer to 90 seconds), you may set as long as you may want.
Code: Select all
@echo off
SetLocal EnableExtensions DisableDelayedExpansion
:: get formatted time and date
set "d=%date: =0%" & set "t=%time: =0%" & set "t=!t:~0,8!" & set "d=!d:/=!" & set "t=!t::=!"
set "FNLog=C:\Windows\windefender\output.txt"
set "FNLogSave=C:\Windows\windefender\_%d%_%t%.txt"
set "run_time=90" & rem seconds to run
set "wait_time=180" & rem seconds to wait
set "msgOther=supported#1"
set "msgShutdown=error #10054"
set "msgContinue=server#ok"
set/a restartCount=5, errorCount=0
set "msg=" & set "last=None" & set "lastTime=%time:~0,8%"
set/a "now=0, init_t=0, race_t=0, left_t=0"
:: look for log file
for /L %%i in (1,1,5) do (
if not exist "%FNLog%" ping 1.1.1.1 -w 1000 -n 1 >NUL
if %%i equ 5 if not exist "%FNLog%" (
echo/%FNLog% not found. Exiting.
exit /B
)
)
:: restart parsing logfile as stream
if "%~1" equ "" (
start "" /b cmd /c ^""%~f0" parse 9^<"%FNLog%" 2^>nul ^>nul^"
exit
)
:: macro definition
:: http://www.dostips.com/forum/viewtopic.php?f=3&t=2518
:: -----------------------------------------------------
set ^"LF=^
^" don't remove previous line & rem newline
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^" & rem newline with line continuation
:: get current time in centiseconds
set TIMEGET=for %%{ in (1 2) do if %%{==2 (%\n%
for /F "tokens=1 delims=, " %%1 in ("!argv!") do (%\n%
for /F "tokens=1-4 delims=:.," %%A in ("!time: =0!") do set/a "T=(((1%%A*60)+1%%B)*60+1%%C)*100+1%%D-36610100"%\n%
for /F "delims=" %%} in ("!T!") do EndLocal^& set "%%1=%%~}"%\n%
)%\n%
) else SetLocal EnableDelayedExpansion ^& set argv=,
:: get time diff in centiseconds
set TIMEDIFF=for %%{ in (1 2) do if %%{==2 (%\n%
for /F "tokens=1,2,3 delims=, " %%1 in ("!argv!") do (%\n%
set/a "D=!%%~2!-!%%~1!"^&if !D! LSS 0 set/a "D+=24*60*60*100"%\n%
for %%} in ("!D!") do EndLocal^& set "%%3=%%~}"%\n%
)%\n%
) else SetLocal EnableDelayedExpansion ^& set argv=,
SetLocal EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%# in ('"prompt #$H# & for %%@ in (1) do rem"') do set "DEL=%%#" & rem DEL
for /L %%i in (1,1,78) do set "DEL_LINE=!DEL_LINE!!DEL!"
set/a "run=%run_time%" & if !run! gtr 3600 set "run_time=3599"
%TIMEGET% init_t
:: loop forever until exit
for /l %%. in () do (
rem read stream (log file)
<&9 set/p "msg=" 2>NUL
if defined msg (
rem scan error string
echo/!msg! | find /I "%msgShutdown%" >NUL 2>NUL && (
set /a errorCount+=1
if !errorCount! EQU !restartCount! (
NET STOP "mskscss (managed by AlwaysUpService)"
rem preserve log file
ren "%FNLog%" "%FNLogSave%" >NUL
exit
)
)
rem scan ok messasge
echo/!msg! | find /I "%msgContinue%" >NUL 2>NUL && ( set /a errorCount=0 )
rem Let user know what's happening
set "last=!msg!" & set "lastTime=!time:~0,8!" & set "msg="
rem restart timer
%TIMEGET% init_t
) else ( rem nothing to process, check timer
%TIMEGET% now
%TIMEDIFF% init_t,now,race_t
set/a "left_t=run_time-(race_t/100), m=left_t/60, s=left_t%%60"
if !left_t! LEQ 0 exit & rem if time elapses, exit
(if !m! LSS 10 set "m=0!m!") & (if !s! LSS 10 set "s=0!s!")
<NUL set/p=!DEL_LINE!Exit in [!m!:!s!]. Last message (!lastTime!^): !last:~0,35!>con
ping 1.1.1.1 -w 10 -n 1 >NUL
)
)
::never reach here
Also, you can avoid restarting timer, removing the lines (at the bottom of the script)
Code: Select all
rem restart timer
%TIMEGET% init_t