Computer Hope

Microsoft => Microsoft DOS => Topic started by: murphyc1000 on March 30, 2019, 11:31:20 AM

Title: Help With Creating A Batch File To Read The 2nd To Last Line Of Text
Post by: murphyc1000 on March 30, 2019, 11:31:20 AM
Hi, I need some help on creating a report file by reading the 2nd to last line of text

Here's some examples below

https://ibb.co/mTf2HnY
https://ibb.co/MZCv8dw

Basically I've tried playing around with a few formulas but none have got me the results I wanted, basically I have directories of  text files, I'm looking at the 2nd to last line of text to get the last number. It's a pain loading up all these files on notepad or notepad ++ and scrolling down to find this out when it would be easier to create a batch file to get the results for me without having to open up these files.

So I'd like to generate a text file called 'report.txt' to gather both the filename and the 2nd to last line of text. However the text files will always be unique and there will be more than one in the directory. The batch file will be in the same directory as the text files so all i'll have to do is run it.

The report file needs to display

Filename1 - 2nd to last line of Text From Filename1
Filename2 - 2nd to last line of Text From Filename2
Filename3 - 2nd to last line of Text From Filename3
etc...

So It'll look something like this

208634168_4.txt - media_1175.ts
208634168_5.txt - media_1200.ts
208634168_6.txt - media_1122.ts
etc...

This is for all text files within the directory.


Thanks
Title: Re: Help With Creating A Batch File To Read The 2nd To Last Line Of Text
Post by: Salmon Trout on March 30, 2019, 03:42:09 PM
So you want the penultimate line...


Title: Re: Help With Creating A Batch File To Read The 2nd To Last Line Of Text
Post by: murphyc1000 on March 30, 2019, 06:06:10 PM
Yes
Title: Re: Help With Creating A Batch File To Read The 2nd To Last Line Of Text
Post by: patio on March 30, 2019, 09:32:31 PM
:P
Title: Re: Help With Creating A Batch File To Read The 2nd To Last Line Of Text
Post by: Salmon Trout on March 31, 2019, 02:24:15 AM

@echo off
setlocal enabledelayedexpansion
if exist report.txt del report.txt
for %%A in (*.txt) do (
    set "lastline="
    set "thisline="
    for /f "delims=" %%B in ('type "%%A"') do (
        set lastline=!thisline!
        set thisline=%%B
        )
    echo %%A !lastline! >> report.txt
    )

Title: Re: Help With Creating A Batch File To Read The 2nd To Last Line Of Text
Post by: murphyc1000 on March 31, 2019, 06:03:40 AM
Thank you very much, I appreciate your help on this one  :D