Computing.Net > Forums > Programming > DOS batch file to read newest text file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

DOS batch file to read newest text file

Reply to Message Icon

Name: Debster
Date: July 23, 2009 at 15:09:44 Pacific
OS: Windows 2003 sp2
CPU/Ram: Xeon 2.5Ghz, 2GB
Product: Microsoft / Dos
Subcategory: Batch
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Debster
Date: July 23, 2009 at 16:29:51 Pacific
Reply:

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)


0

Response Number 2
Name: Debster
Date: July 23, 2009 at 16:37:04 Pacific
Reply:

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)


0

Response Number 3
Name: Mechanix2Go
Date: July 23, 2009 at 22:21:43 Pacific
Reply:

if "!lastline!"=="Exiting..." (
echo Graceful exit
) else (
echo No Graceful exit found
)


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

M2


0

Response Number 4
Name: Debster
Date: July 24, 2009 at 09:24:50 Pacific
Reply:

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?


0

Response Number 5
Name: Debster
Date: July 24, 2009 at 10:24:20 Pacific
Reply:

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
)


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: July 26, 2009 at 02:26:12 Pacific
Reply:

That's a good one to keep in mind.


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

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: DOS batch file to read newest text file

batch script to parse text file www.computing.net/answers/programming/batch-script-to-parse-text-file/16793.html

DOS Batch File Commands www.computing.net/answers/programming/dos-batch-file-commands/14637.html

editing dos batch file www.computing.net/answers/programming/editing-dos-batch-file/20208.html