Modify batch script to conditionally write files -
i have batch file below copied internet. need it. batch takes output command or other batch , echos output both console , specified file. echoing console fine there way write file when condition met? may if echoed text contains string "logmessage". if 2 messages echoed command or batch, message 1: "hello, how you?" message 2: "logmessage thanks, good."
the message 1 should displayed on console , message 2 should displayed both on console , in file.
@echo off :: check windows version if not "%os%"=="windows_nt" goto syntax :: keep variables local setlocal :: check command line arguments set append=0 if /i [%1]==[-a] ( set append=1 shift ) if [%1]==[] goto syntax if not [%2]==[] goto syntax :: test invalid wildcards set counter=0 /f %%a in ('dir /a /b %1 2^>nul') call :count "%%~fa" if %counter% gtr 1 ( set counter= goto syntax ) :: valid filename seems have been specified set file=%1 :: check if directory specified name exists dir /ad %file% >nul 2>nul if not errorlevel 1 ( set file= goto syntax ) :: specify /y switch windows 2000 / xp copy command set y= ver | find "windows nt" > nul if errorlevel 1 set y=/y :: flush existing file or create new 1 if -a wasn't specified if %append%==0 (copy %y% nul %file% > nul 2>&1) :: actual tee /f "tokens=1* delims=]" %%a in ('find /n /v ""') ( > con echo.%%b >> %file% echo.%%b ) :: done endlocal goto:eof :count set /a counter += 1 set file=%1 goto:eof :syntax echo. echo tee.bat, version 2.11a windows nt 4 / 2000 / xp echo display text on screen , redirect file simultaneously echo. if not "%os%"=="windows_nt" echo usage: some_command ¦ tee.bat [ -a ] filename if not "%os%"=="windows_nt" goto skip echo usage: some_command ^| tee.bat [ -a ] filename :skip echo. echo where: "some_command" command output should redirected echo "filename" file output should redirected echo -a appends output of command file, echo rather overwriting file echo. echo. echo written rob van der woude echo http://www.robvanderwoude.com echo modified kees couprie echo http://kees.couprie.org echo , andrew cameron
... :: actual tee /f "tokens=1* delims=]" %%a in ('find /n /v ""') ( > con echo.%%b echo "%%b"|find "logmessage" >nul if errorlevel 1 >> %file% echo.%%b ) ...
the find attempts locate target string in message recorded. if found, errorlevel
set 0, non-0 otherwise.
do not try use syntax "if %errorlevel%==0"
here since it's in statement block , construct not interpret errorlevel find, stood when for
parsed.
suppress "don't write log" flag, try
:: actual tee setlocal enabledelayedexpansion /f "tokens=1* delims=]" %%a in ('find /n /v ""') ( set message=%%b if defined message set message=!message:logmessage=!
con echo(!message! if "%%b"=="!message!" >> %file% echo(%%b ) endlocal
which should delete string "logmesssage" (in either case) , - bit seems reversed - not log line file, send line, flag "logmessage" expunged, console. perhaps "skiplog" better string...
Comments
Post a Comment