I'm trying to read a log file with a batch file.
- If the batch file finds
SUCCESS
in the log file, the text should be in green. - If the batch file finds
WARNING
in the log file, the text should be in cyan. - If the batch file finds
ERROR
in the log file, the text should be in red.
It works when it finds either of the one value, but if the log file contains two or more of the different results like SUCCESS
and WARNING
it doesn't work.
Trying to read a log file with batch file on Windows.
@echo off
set LogPath=C:\Mworks\logs
set /p MM-DD="Enter Month and Day (MM-DD) to Search logs for success close eg. 08-24: "
set YEAR=2019
@echo searching for %LogPath%\%YEAR%-%MM-DD%*.log
FOR /F "eol=- tokens=1-8 delims= " %%i in ('find /I "LOG ROTATE COMPLETE" %LogPath%\%YEAR%-%MM-DD%*.log') do set result=%%p
echo %result%
(
IF /I %result%==ERROR (goto :ERROR)
)
(
if /I %result%==SUCCESS (goto :SUCCESS)
)
(
if /I %result%==WARNING (goto :WARNING)
)
REM somehow need to catch value at token 8 and color the lines accordingly
REM proper use of enableDelayedExpansion might help but it's quite tough simply with batch script.
REM I've seen hackoo's version of pinger doing it but the code is hard to understand, which part controls what.
:SUCCESS
color 0A
REM this line needs to be on GREEN
FOR /F "eol=- tokens=1-8 delims= " %%i in ('find /I "LOG ROTATE COMPLETE" %LogPath%\2019-%MM-DD%*.log') do @echo %%i %%j %%k %%l %%m %%n %%o %%p
goto end
:ERROR
color 0C
REM this line nees to be on RED
FOR /F "eol=- tokens=1-8 delims= " %%i in ('find /I "LOG ROTATE COMPLETE" %LogPath%\2019-%MM-DD%*.log') do @echo %%i %%j %%k %%l %%m %%n %%o %%p
goto end
:WARNING
REM This line needs to be on CYAN
color 0B
FOR /F "eol=- tokens=1-8 delims= " %%i in ('find /I "LOG ROTATE COMPLETE" %LogPath%\2019-%MM-DD%*.log') do @echo %%i %%j %%k %%l %%m %%n %%o %%p
:end
pause
The code doesn't work if it find more than one result where result could be SUCCESS
, WARNING
, ERROR
.
**LOG BEGUN 2019-08-24 03:42:28,662
loading c:Mworksconfiglog4j2.xml
INFO 2019-08-24 03:42:34,100 Initializing configs... :: oracle.retail.mworks.config.mworksProperties [mworks]
INFO 2019-08-24 03:42:34,100 Loading Properties - jar:file:/C:/Mworks/lib/menv-engine.jar!/dtv/res/config/actions.properties :: dtv.util.ResourceUtils [mworks]
INFO 2019-08-24 03:42:34,115 Loading Properties - file:/C:/Mworks/cust_config/version1/actions.properties :: dtv.util.ResourceUtils [mworks]
INFO 2019-08-24 03:42:34,131 Loading Properties - jar:file:/C:/Mworks/lib/menv-engine.jar!/dtv/res/config/environment.properties :: dtv.util.ResourceUtils [mworks]
INFO 2019-08-24 03:42:34,131 Loading Properties - file:/C:/Mworks/cust_config/version1/environment.properties :: dtv.util.ResourceUtils [mworks]
INFO 2019-08-24 03:42:34,146 Loading Properties - jar:file:/C:/Mworks/lib/menv-engine.jar!/dtv/res/config/update.properties :: dtv.util.ResourceUtils [mworks]
INFO 2019-08-24 03:42:34,162 Loading Properties - file:/C:/Mworks/cust_config/version1/update.properties :: dtv.util.ResourceUtils [mworks]
INFO 2019-08-24 03:42:34,162 Loading Properties - jar:file:/C:/Mworks/lib/menv-engine.jar!/dtv/res/config/local.properties :: dtv.util.ResourceUtils [mworks]
INFO 2019-08-24 03:42:34,162 Loading Properties - file:/C:/Mworks/cust_config/version1/local.properties :: dtv.util.ResourceUtils [mworks]
INFO 2019-08-24 03:42:34,584 Loading registration data from c:\Mworks\res\data\registrationdata.json :: oracle.retail.mworks.registration.RegistrationDataManager [mworks]
INFO 2019-08-24 03:42:35,287 Gathering local Client data. :: oracle.retail.mworks.registration.RegistrationDataManager [mworks]
INFO 2019-08-24 03:42:36,334 loading jar:file:/C:/Mworks/lib/menv-engine.jar!/dtv/res/config/MBeanInfoConfig.xml :: dtv.util.config.ConfigHelper [mworks]
INFO 2019-08-24 03:42:36,883
INFO 2019-08-24 03:42:36,883 Waiting for services to start... :: oracle.retail.mworks.mworks [mworks]
ntly running actions: [startup-lead, create-update-directories, LOG ROTATE] :: oracle.retail.mworks.action.Action [ActionExec-1]
INFO 2019-08-24 03:42:40,447 Action [CreateUpdateDirectories :: oracle.retail.mworks.atoms.CreateUpdateDirectories] complete. State: SUCCESS, Result: -----------------------------------
The text below should be in RED
----------------------------------
INFO 2019-08-24 03:42:40:03,060 LOG ROTATE complete. Status: ERROR Created update directories. :: oracle.retail.mworks.atoms.Atom [ActionExec-1]
INFO 2019-08-24 03:42:40,447 Currently running actions: [startup-lead, LOG ROTATE] :: oracle.retail.mworks.action.Action [ActionExec-1]
INFO 2019-08-24 03:42:40,447 Action [create-update-directories] returned state [SUCCESS] with message [Created update directories.] ::
The text below should be in cyan
---------------------------------------
INFO 2019-08-24 04:44:03,060 LOG ROTATE complete. Status: WARNING
LOT OF lines DELETED
The text below should be in green
----------------------------------------
INFO 2019-08-24 05:44:03,060 LOG ROTATE complete. Status: SUCCESS :: oracle.retail.xenvironment.action.Action [ActionExec-2]
sample log
something like this:
@Echo Off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
call :chooseColor 0A "This is colored Green means SUCCESS"
echo.
call :chooseColor 0B "This is colored Cyan means WARNING"
echo.
call :chooseColor 0C "This is colored Red means ERROR"
echo.
pause
goto eof
:chooseColor
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i
:eof