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

julianfiglioli

New member
Local time
8:30 AM
Messages
3
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 My Computer

At a glance

Windows 7
Computer type
PC/Desktop
OS
Windows 7
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 My Computer

At a glance

Windows 7 Pro 32Intel(R) Core(TM) i5 CPU M 540 @ 2.53GHz4,00 GB (Usable 2,98)NVIDIA NVS 5100M
Computer type
Laptop
Computer Manufacturer/Model Number
HP Elitebook 8540p
OS
Windows 7 Pro 32
CPU
Intel(R) Core(TM) i5 CPU M 540 @ 2.53GHz
Motherboard
Hewlett-Packard 1521
Memory
4,00 GB (Usable 2,98)
Graphics Card(s)
NVIDIA NVS 5100M
Sound Card
NVIDIA High Definition Audio
Screen Resolution
1600x900
Hard Drives
INTEL SSDSA2CW120G3
Antivirus
F-Secure Internet Security
Browser
IE, Firefox, Opera
Other Info
Sandboxie,
SRP (Software Restriction Policy),
EMET (Enhanced Mitigation Experience Toolkit),
WFC (Windows Firewall Control by BiniSoft),
Malwarebytes Premium
Back
Top