Read last line from newest file in folder

June 3, 2009 at 11:15:00
Specs: Windows XP
How do I read the last line of text from the newest log file in a particular directory?

See More: Read last line from newest file in folder

Report •


#1
June 3, 2009 at 11:39:16
@echo off & setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/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 at first you don't succeed, you're about average.

M2


Report •

#2
June 3, 2009 at 17:30:01
if you have Python on windows:

import os
path=os.path.join("c:\\","test")
os.chdir(path)
temp=0
for files in os.listdir("."):
    if os.path.isfile(files):
        mtime=os.path.getmtime(files)
        if mtime>temp:
            temp=mtime
            filename=files
print open(filename).readlines()[-1].strip() #print last line


save the code as myscript.py and on command line
c:\test> python myscript.py


Report •

#3
June 3, 2009 at 19:15:00
Mechanix2Go,

Thanks so much for the great code and quick response. 1 thing I forgot to mention was that the directory is in a different location than the script will be. How would I specify the full directory path? Also, I have been looking for a reference for the various switches like %%i and %%a for the 'for' command. Do you know of one?

ghostdog,
I don't have python, but thanks anyway.


Report •

Related Solutions

#4
June 3, 2009 at 21:55:06
oops, submitted twice

Report •

#5
June 4, 2009 at 06:20:59
Precede the for loop with:

pushd d:\dir\where\my\files\are


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

M2


Report •


Ask Question