Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Have seen some wonderful posts here. I need a batch file to read the last line of the latest log file (txt) in dir. If last line not xx, send email. I've got the email part working but need help determining latest log filename and evaluating last line.
Any advice much appreciated.

Found something to get me started but my IF/ELSE isn't working :( My IF doesn't eval properly (never returns true) and I get runtime "ELSE is not recognized as an internal or external command...."
Anyone see what's wrong? Many thanks!
@echo off & setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in ('dir *.log /b/o-d') do (
set newest=%%a
goto :last
):last
for /f "tokens=* delims= " %%i in (!newest!) do (
set lastline=%%i
)echo last line in !newest! is ==!lastline!==
if not !lastline! == "Exiting..." (echo No graceful exit)
else (echo Graceful exit found)

Pls forgive me I am newbie to research via forums. My IF trouble is that when log's last line = "Exiting..." below IF stmt doesn't eval TRUE.
if !lastline! == "Exiting..." (echo Graceful exit)
else (echo No Graceful exit found)

if "!lastline!"=="Exiting..." (
echo Graceful exit
) else (
echo No Graceful exit found
)
=====================================
If at first you don't succeed, you're about average.M2

Awesome. Now what if the search string contains quotes?
2quotes returns both true and false which seems impossible
if "!lastline!"==""Exiting..."" (
echo Graceful exit
) else (
echo No Graceful exit found
)3quotes the sql way returns false
if "!lastline!"=="""Exiting...""" (
echo Graceful exit
) else (
echo No Graceful exit found
)same for single quote + double, returns false
if "!lastline!"=='"(main proc) complete, exiting"' (
echo Graceful exit
) else (
echo No Graceful exit
)Sure I could strip the " from string but is there a shorter way?

Found a solution. In case anyone is looking, below is corrected code. Thanks M2. Found many of yr responses on other thread which helped me solve my issue.
@echo off & setLocal EnableDelayedExpansion
set chkstr="(main proc) complete, exiting"
for /f "tokens=* delims= " %%a in ('dir *.txt /b/o-d') do (
set newest=%%a
goto :last
):last
for /f "tokens=* delims= " %%i in (!newest!) do (
set lastline=%%i
)echo last line in !newest! is ==!lastline!==
set str=!lastline!
for /f "useback tokens=*" %%a in ('%str%') do set str=%%~a
for /f "useback tokens=*" %%a in ('%chkstr%') do set chkstr=%%~a
echo.%str%
echo.%chkstr%if !str!==!chkstr! (
echo Graceful exit
) else (
echo No Graceful exit
)

That's a good one to keep in mind.
=====================================
If at first you don't succeed, you're about average.M2

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |