DOS batch file to read newest text file

Microsoft / Dos
July 23, 2009 at 15:09:44
Specs: Windows 2003 sp2, Xeon 2.5Ghz, 2GB
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.


See More: DOS batch file to read newest text file

Report •


#1
July 23, 2009 at 16:29:51
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)


Report •

#2
July 23, 2009 at 16:37:04
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)


Report •

#3
July 23, 2009 at 22:21:43
if "!lastline!"=="Exiting..." (
echo Graceful exit
) else (
echo No Graceful exit found
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions

#4
July 24, 2009 at 09:24:50
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?


Report •

#5
July 24, 2009 at 10:24:20
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
)


Report •

#6
July 26, 2009 at 02:26:12
That's a good one to keep in mind.


=====================================
If at first you don't succeed, you're about average.

M2


Report •


Ask Question