Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Help With Creating A Batch File To Read The 2nd To Last Line Of Text  (Read 4438 times)

0 Members and 1 Guest are viewing this topic.

murphyc1000

    Topic Starter


    Starter

    • Experience: Familiar
    • OS: Windows 10
    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

    Salmon Trout

    • Guest
    Re: Help With Creating A Batch File To Read The 2nd To Last Line Of Text
    « Reply #1 on: March 30, 2019, 03:42:09 PM »
    So you want the penultimate line...



    murphyc1000

      Topic Starter


      Starter

      • Experience: Familiar
      • OS: Windows 10
      Re: Help With Creating A Batch File To Read The 2nd To Last Line Of Text
      « Reply #2 on: March 30, 2019, 06:06:10 PM »
      Yes

      patio

      • Moderator


      • Genius
      • Maud' Dib
      • Thanked: 1769
        • Yes
      • Experience: Beginner
      • OS: Windows 7
      Re: Help With Creating A Batch File To Read The 2nd To Last Line Of Text
      « Reply #3 on: March 30, 2019, 09:32:31 PM »
      :P
      " Anyone who goes to a psychiatrist should have his head examined. "

      Salmon Trout

      • Guest
      Re: Help With Creating A Batch File To Read The 2nd To Last Line Of Text
      « Reply #4 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
          )


      murphyc1000

        Topic Starter


        Starter

        • Experience: Familiar
        • OS: Windows 10
        Re: Help With Creating A Batch File To Read The 2nd To Last Line Of Text
        « Reply #5 on: March 31, 2019, 06:03:40 AM »
        Thank you very much, I appreciate your help on this one  :D