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
julianfiglioli

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
)

endlocal


My System SpecsSystem Spec
.
29 Apr 2015   #2
Tookeri

Windows 7 Pro 32
 
 

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

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




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
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...
Software
.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...
Software
How to read Log File
Hello, I would really appreciate your help with this. Im a bit of a newbie so bear with me. I ran the SFC scannow and received my log file (It's attached), but I cant read what exactly is wrong with my system. The reason I ran the SFC Scan is because when Im on the internet and there are 2...
Performance & Maintenance
Command Line Prompt - How To Export List of Files to Text File
Hi guys, I have a folder with 995 files in it and I'd like to give someone a text file with a list of all these files in it. Is there a way that I can get the command line to send the names of these files to a text file? I tried navigating to the directory and using the DIR command and,...
General Discussion
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 17:28.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App