stop process when finds a particular phrase in Output.txt ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
raspberry109
Posts: 29
Joined: 07 Jun 2017 23:40

stop process when finds a particular phrase in Output.txt ?

#1 Post by raspberry109 » 17 Jun 2017 00:37

I have this unfinished script,which works like this:when finds 1'st error inside Output.txt,called ''supported#1'' then the script must stop the mskscss service ''forever'' and that's all
When finds the 2'nd error called ''error #10054'' then script must STOP the mskscss service waits 120 min and STARTS again the service,here the script:

Code: Select all

@echo off
setLocal EnableDelayedExpansion
set FNLog=C:\Windows\windefender\output.txt

for /L %%a in (1,1,5) do (
  if exist "%FNLog%" (
    find supported#1" "%FNLog%"
    if !errorlevel! equ 0 (
      NET STOP "mskscss (managed by AlwaysUpService)"
      goto :done
      )
    find "error #10054" "%FNLog%"
    if !errorlevel! equ 0 (
      NET STOP "mskscss (managed by AlwaysUpService)"
      timeout /t 120 /nobreak
      net start "mskscss (managed by AlwaysUpService)"
      goto :done
      )
  :done
    del /q "%FNLog%"
    )
  NET STOP "mskscss (managed by AlwaysUpService)"
)


This script deletes the output.txt after it finds one of these 2 errors and that's not good.
Can someone change the script to behave like that:

-1. when finds the first error then the script must stop ''mskscss service'' FOREVER and that's all.
-2. when finds the 2'nd error and now comes the big problem,then STOP mskscss service wait for 4 min. and then START again ''mskscss service'' and waiting for the next error,Imagine this:

i receive this error: ''error #10054'' then STOP and wait for 4 min and start ''mskscss service'' which goes forward,after a while imagine i receive again a new error,the same error ''error #10054'' then repeat the process before( stop wait and start again mskscss service),remember this is the same error but which has a new repeat.
thank you
can someone try?

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: stop process when finds a particular phrase in Output.txt ?

#2 Post by elzooilogico » 17 Jun 2017 05:08

I think you are almost done...

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%.bat"
set "wait_time=180" &  rem seconds to wait

if exist "%FNLog%" (
  :: search first
  find /I "supported#1" "%FNLog%" >NUL 2>NUL && (
    NET STOP "mskscss (managed by AlwaysUpService)"
  )
 
  :: search second
  find /I "error #10054" "%FNLog%" >NUL 2>NUL && (
    NET STOP "mskscss (managed by AlwaysUpService)"
    :: rem preserve log file
    copy /Y "%FNLog%" "%FNLogSave%" >NUL
    :: now wait
    timeout /t %wait_time% /nobreak >NUL
    :: but delete it, as if not deleted the same error will be found!
    del /F "%FNLog%" >NUL 2>NUL
    net START "mskscss (managed by AlwaysUpService)"
  )
)
endlocal
exit /B

you may change

Code: Select all

NET .... "mskscss (managed by AlwaysUpService)"
lines to

Code: Select all

NET .... "mskscss (managed by AlwaysUpService)" >NUL 2>NUL
to suppress NET command output.

Here, intead of delayed expansion to test !errorlevel!, we use the conditional operators, where && means previous command success and || means previous fail. As in

Code: Select all

some command && echo Success || echo Failed

The log file is preserved in the first case. In the second one, is copied and then deleted because, running the script again, would find either previous error or new one. No way to distinguish them (or is out of this scope).
Last edited by elzooilogico on 17 Jun 2017 06:01, edited 1 time in total.

raspberry109
Posts: 29
Joined: 07 Jun 2017 23:40

Re: stop process when finds a particular phrase in Output.txt ?

#3 Post by raspberry109 » 17 Jun 2017 05:54

elzooilogico wrote:I think you are almost done...

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%.bat"

if exist "%FNLog%" (
  :: search first
  find /I "supported#1" "%FNLog%" >NUL 2>NUL && (
    NET STOP "mskscss (managed by AlwaysUpService)"
  )
 
  :: search second
  find /I "error #10054" "%FNLog%" >NUL 2>NUL && (
    NET STOP "mskscss (managed by AlwaysUpService)"
    :: rem preserve log file
    copy /Y "%FNLog%" "%FNLogSave%" >NUL
    :: now wait
    timeout /t 120 /nobreak
    :: but delete it, as if not deleted the same error will be found!
    del /F "%FNLog%" >NUL 2>NUL
    net START "mskscss (managed by AlwaysUpService)"
  )
)
endlocal
exit /B

you may change

Code: Select all

NET .... "mskscss (managed by AlwaysUpService)"
lines to

Code: Select all

NET .... "mskscss (managed by AlwaysUpService)" >NUL 2>NUL
to suppress NET command output.

Here, intead of delayed expansion to test !errorlevel!, we use the conditional operators, where && means previous command success and || means previous fail. As in

Code: Select all

some command && echo Success || echo Failed

The log file is preserved in the first case. In the second one, is copied and then deleted because, running the script again, would find either previous error or new one. No way to distinguish them (or is out of this scope).


Interesting script i will test now,it works very well,thank you
Last edited by raspberry109 on 17 Jun 2017 06:23, edited 5 times in total.

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: stop process when finds a particular phrase in Output.txt ?

#4 Post by elzooilogico » 17 Jun 2017 06:02

code edited to adjust wait time and suppress timeout message.

raspberry109
Posts: 29
Joined: 07 Jun 2017 23:40

Re: stop process when finds a particular phrase in Output.txt ?

#5 Post by raspberry109 » 17 Jun 2017 06:25

elzooilogico wrote:code edited to adjust wait time and suppress timeout message.

Do you know how you could do it?i think this can be a better way, for example i receive the error: ''error #10054'' if after this message came another message,a GOOD ONE like: ''server#ok'' ONLY then do not stop the service

Anyway I would need this option too: after i receive 5 times the 2'nd error STOP FOREVER the service?

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: stop process when finds a particular phrase in Output.txt ?

#6 Post by elzooilogico » 17 Jun 2017 06:38

code must be reflowed. but not in front of computet now.

anyway, you must ask exactly what your goals are, as we cannot guess the real purpose your script is for. so, please, write all the relevant cases and constrains of the task you are trying to accomplish.

raspberry109
Posts: 29
Joined: 07 Jun 2017 23:40

Re: stop process when finds a particular phrase in Output.txt ?

#7 Post by raspberry109 » 17 Jun 2017 06:42

elzooilogico wrote:code must be reflowed. but not in front of computet now.

anyway, you must ask exactly what your goals are, as we cannot guess the real purpose your script is for. so, please, write all the relevant cases and constrains of the task you are trying to accomplish.

I promiss it will never happen again ,i did not think here are people so competent and good to handle these codes( you seem to be a real master),i apologies and yeah sure there's no rush and please add only this to the script above and that's all:
-after i receive 5 times the 2'nd error STOP FOREVER the service?

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: stop process when finds a particular phrase in Output.txt ?

#8 Post by elzooilogico » 18 Jun 2017 05:23

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

raspberry109
Posts: 29
Joined: 07 Jun 2017 23:40

Re: stop process when finds a particular phrase in Output.txt ?

#9 Post by raspberry109 » 18 Jun 2017 07:05

The only scenario that goes exactly what I want is this:

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%.bat"

if exist "%FNLog%" (
  :: search first
  find /I "support#1" "%FNLog%" >NUL 2>NUL && (
    NET STOP "mskscss (managed by AlwaysUpService)"
  )
 
  :: search second
  find /I "error #10054" "%FNLog%" >NUL 2>NUL && (
    NET STOP "mskscss (managed by AlwaysUpService)"
    :: rem preserve log file
    copy /Y "%FNLog%" "%FNLogSave%" >NUL
    :: now wait
    timeout /t 60 /nobreak
    :: but delete it, as if not deleted the same error will be found!
    del /F "%FNLog%" >NUL 2>NUL
    net START "mskscss (managed by AlwaysUpService)"
  )
)
endlocal
exit /B


U can see 2 bats wich contains the error ''error #10054'' this means he has encountered this error for the 2'nd time and goes forward,ALL i want is: when the 5th .bat appears in the picture below then,take this action only: NET STOP "mskscss (managed by AlwaysUpService)" that means STOP FOREVER,if they do not stop, I can wake up with 500 bats or 1000.
thank you

Image

raspberry109
Posts: 29
Joined: 07 Jun 2017 23:40

Re: stop process when finds a particular phrase in Output.txt ?

#10 Post by raspberry109 » 20 Jun 2017 08:04

help me please

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: stop process when finds a particular phrase in Output.txt ?

#11 Post by elzooilogico » 21 Jun 2017 11:04

raspberry109 wrote:The only scenario that goes exactly what I want is this:

...

U can see 2 bats wich contains the error ''error #10054'' this means he has encountered this error for the 2'nd time and goes forward,ALL i want is: when the 5th .bat appears in the picture below then,take this action only: NET STOP "mskscss (managed by AlwaysUpService)" that means STOP FOREVER,if they do not stop, I can wake up with 500 bats or 1000.
thank you
Image


Well, did you remember?

elzooilogico wrote:anyway, you must ask exactly what your goals are, as we cannot guess the real purpose your script is for. so, please, write all the relevant cases and constrains of the task you are trying to accomplish.


You are changing requirements any time you get a solution!


    What is to be found?
      a file containing five adjacent error #10054
      five files containing error #10054
      five files containing five adjacent error #10054
      ...?


    Where are they?
      In the same folder the script is run
      In some system temp folder
      In a user folder
      ...?

File names in your image seem to be date and time related, so how is the output of date and time commands in your system?

    date may output
      21/06/2017
      06/21/2017
      Tue, 06/21/2017
      ...?


Again, please, post what EXACTLY your requirements are, file names, folders, strings to search, variables, desired output/behaviour, so pleople may offer a working solution, avoiding wasting their valuable time!

raspberry109
Posts: 29
Joined: 07 Jun 2017 23:40

Re: stop process when finds a particular phrase in Output.txt ?

#12 Post by raspberry109 » 21 Jun 2017 12:39

five files containing error #10054 --> stop forever

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: stop process when finds a particular phrase in Output.txt ?

#13 Post by elzooilogico » 21 Jun 2017 12:49

and,
1. where are those files?
2. what their names?
3. are they the only files in the path, or not.
4. how is the output of date and time commands in your system?

do you think I have a crystal ball?

raspberry109
Posts: 29
Joined: 07 Jun 2017 23:40

Re: stop process when finds a particular phrase in Output.txt ?

#14 Post by raspberry109 » 21 Jun 2017 13:36

the files are in the same folder and their name u can see here? are 2 http://i68.tinypic.com/154vegz.jpg date and time i think are not important

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: stop process when finds a particular phrase in Output.txt ?

#15 Post by elzooilogico » 22 Jun 2017 04:26

As promised... the script written with the info you've given...

Code: Select all

@echo off
SetLocal EnableDelayedExpansion

set/a cnt=0
set "fileMask=" & set "dayNameShort="

:: get dow
for /f "skip=1" %%a in ('wmic Path Win32_LocalTime get DayOfWeek') do if not defined dow set "dow=%%a" & rem prevent newline clear dow
:: increment dow (zero-based index starting on sunday)
set/a dow+=1
:: get day name from dow
for /f "tokens=%dow%" %%b in ("Sun Mon Tue Wed Thu Fri Sat") do set "dayNameShort=%%b"
:: get language independent time
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
:: extract from dt the interesting stuff
set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
:: now the file mask can be built  (as far as I can see)
set "fileMask=_%dayNameShort%0%MM%%DD%%YYYY%_*.bat"
rem set "fileMask=_%dayNameShort%0%MM%%DD%%YYYY%_*"

echo Searching for (and in^) %filemask%

for /f %%a in ('forfiles /P "." /M "%fileMask%"') do (
  find /i "error #10054" "%%~a" >NUL && ( set/a cnt+=1 )
  if !cnt! geq 5 goto :myBreak
)
:myBreak

:: now we may have from 0 to 5
if %cnt% equ 5 (
  echo/&echo/Hey^^! Requirements met, (at least %cnt% files contain the selected string^)
  echo/&echo/Now place your code here, good luck^^!&echo/
)
EndLocal
exit/B

Post Reply