How I read a file .txt and put the last line in a variable


  1. Posts : 3
    Windows 7
       #1

    How I read a file .txt and put the last line in a variable


    People, I am from Brazil. My English is not very well! Sorry.

    I need read a file .txt, where, I need put the last line in a variable.

    I am tryng find de number of lines and with a FOR /F read line and line.

    But not this going right.

    @echo off
    setlocal enableextensions enabledelayedexpansion

    set/p n=<teste.txt

    REM ### Here I cat the number of the lines ###
    type teste.txt | find /v /c "" <teste.txt >nlinhas
    set/p l=<nlinhas

    REM ### Variable that sum ###
    set num=0

    REM ### Read line and line ###
    FOR /F %%n in (teste.txt) do (

    if %num%==%l% (
    echo %%n
    goto :fim
    )
    set/a num=num+1 <== Where I put this code, occurs syntax error
    )

    endlocal
      My Computer


  2. Posts : 1,049
    Windows 7 Pro 32
       #2

    This should be enough:

    @ECHO OFF
    for /f "tokens=*" %%a in (teste.txt) do SET LINE=%%a
    echo %LINE%

    Every new read line will set the variable LINE. When all lines are read, you'll end up with the last line as the value.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 03:12.
Find Us