Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.

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

29 Apr 2015   #1

Windows 7
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


My System SpecsSystem Spec
29 Apr 2015   #2

Windows 7 Pro 32

This should be enough:

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 System SpecsSystem Spec

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

Thread Tools

Similar help and support threads
Thread Forum
Changing a file to read-only does not remain read-only
I'm having the opposite problem from most of what I have read here. I am trying to set a file to read-only. Each time I do, the next time I open explorer the check-mark is gone making it writable to again. The file is an LSO that (if I delete it) regenerates itself. I have tried to change the...
General Discussion
Can a property from a .properties file be made into a usable variable?
I have code to read out lines from .properties, but it just prints it out, because im an idiot and didn't notice till now. So, the question is, can it be made into a variable instead? current code; Properties prop = new Properties(); InputStream input = null; try { input = new...
.bat file, batch coding simple variable use
@echo off color C SET IPaddress=0 set /p IPaddress=Please enter the destination Ip Address: color A ping IPaddress pause okay, so i want the IPaddress variable to be used as the ipaddress to be pinged. When i run this, it is ineffective. Can someone teach me as why and how to properly code...
Need help creating a .bat file moving output into a variable
Hello everyone, I am looking to create a .bat file to automate task that I need to do about a thousand times. I have some experience with .bat files, however I would still consider myself a beginner. I also didn't really see a good forum for something like this so if it need to be moved I...
General Discussion
Clicking anywhere on file line selects file
I'm trying to do things like select multiple files (or folders), paste a file, or create a new file/folder but if I click anywhere on the line, it selects the file as if I was clicking on the file name. It means I have to click in a blank area (outside of a column or line) to do any of this stuff...
General Discussion

Our Sites

Site Links

About Us

Find 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 23:49.
Twitter Facebook Google+ Seven Forums iOS App Seven Forums Android App