PC Review


Reply
Thread Tools Rate Thread

Display last line of a text file

 
 
fpschultze
Guest
Posts: n/a
 
      16th Dec 2005
Hello.

Here comes a batch file that shows how to read the last line of a text
file:

=====
@Echo Off

If %1!==! (
Echo Read and display the last line of a text file
Echo.
Echo Syntax: %~n0 filename.txt
Goto :EOF)

If Not Exist %1 (
Echo File not found - %1
Goto :EOF)

SetLocal

::String that will be used to identify the last line
Set
_=vb043527nc502374v520345c7n20345vb7230457c2n305v72b345v02n3v50237cb230b5v

::Copy the file to the temp folder
Copy %1 %temp%.\tmp.tmp >NUL

::Append unique identifier string to the end of the temp file
Echo %_% >> %temp%.\tmp.tmp

:etermine the line number of the last line
For /F "delims=[]" %%a In ('Find /N "%_%" ^< %temp%.\tmp.tmp') Do Set
_=%%a

::Set lastline variable to the line next to last line of the original
file
Set /A _ -= 2
For /F "Tokens=* Skip=%_%" %%A In (%1) Do Set lastline=%%a

::Cleanup
If Exist %temp%.\tmp.tmp Del %temp%.\tmp.tmp

:isplay line
Echo %lastline%

EndLocal
=====

Have fun

--
Frank-Peter Schultze, http://www.fpschultze.de

 
Reply With Quote
 
 
 
 
foxidrive
Guest
Posts: n/a
 
      16th Dec 2005
On 16 Dec 2005 07:02:20 -0800, fpschultze wrote:

> Hello.
>
> Here comes a batch file that shows how to read the last line of a text
> file:


Does W2K have the more extensions of XP?

@echo off
setlocal EnableExtensions
for /f %%a in ('find /c /v "" ^< %1') do set /a var=%%a-1
more +%var% %1
 
Reply With Quote
 
Harlan Grove
Guest
Posts: n/a
 
      16th Dec 2005
Seems a lot of work just to find the last line. Brute force seems to work
for me.

for /f "delims=" %%s in (%1) do set LASTLINE=%%s
echo %LASTLINE%

Maybe I don't understand the problem. Also, if the set statement above
doesn't work, there's always echo %%s > %TEMP%\lastline.tmp, then reading
that file's only line into LASTLINE. I don't understand the need to run
FIND.


 
Reply With Quote
 
Timo Salmi
Guest
Posts: n/a
 
      16th Dec 2005
23} How do I get the n'th, the first and the last line of a text file?
24} How do I get the m'th item on the n'th line of a text file?

144994 Dec 7 2005 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
private.php?do=newpm&u= <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html
 
Reply With Quote
 
foxidrive
Guest
Posts: n/a
 
      17th Dec 2005
On Fri, 16 Dec 2005 09:13:32 -0800, Harlan Grove wrote:

> Seems a lot of work just to find the last line. Brute force seems to work
> for me.
>
> for /f "delims=" %%s in (%1) do set LASTLINE=%%s
> echo %LASTLINE%
>
> Maybe I don't understand the problem. Also, if the set statement above
> doesn't work, there's always echo %%s > %TEMP%\lastline.tmp, then reading
> that file's only line into LASTLINE. I don't understand the need to run
> FIND.


It just shows that there's more than one way to skin a cat.
 
Reply With Quote
 
Gary Smith
Guest
Posts: n/a
 
      18th Dec 2005
In alt.msdos.batch.nt Harlan Grove <(E-Mail Removed)> wrote:
> Seems a lot of work just to find the last line. Brute force seems to work
> for me.


> for /f "delims=" %%s in (%1) do set LASTLINE=%%s
> echo %LASTLINE%


> Maybe I don't understand the problem. Also, if the set statement above
> doesn't work, there's always echo %%s > %TEMP%\lastline.tmp, then reading
> that file's only line into LASTLINE. I don't understand the need to run
> FIND.


Both SET and ECHO have problems when the last line contains "<" or ">".

--
Gary L. Smith
Columbus, Ohio
 
Reply With Quote
 
Al Dunbar
Guest
Posts: n/a
 
      18th Dec 2005

"foxidrive" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Fri, 16 Dec 2005 09:13:32 -0800, Harlan Grove wrote:
>
>> Seems a lot of work just to find the last line. Brute force seems to work
>> for me.
>>
>> for /f "delims=" %%s in (%1) do set LASTLINE=%%s
>> echo %LASTLINE%
>>
>> Maybe I don't understand the problem. Also, if the set statement above
>> doesn't work, there's always echo %%s > %TEMP%\lastline.tmp, then reading
>> that file's only line into LASTLINE. I don't understand the need to run
>> FIND.

>
> It just shows that there's more than one way to skin a cat.


LOL. And sometimes skinning a cat is a good description of the convolutions
it takes to do some things in batch!

/Al


 
Reply With Quote
 
Clay Calvert
Guest
Posts: n/a
 
      18th Dec 2005
On Sun, 18 Dec 2005 03:28:14 -0000, Gary Smith <(E-Mail Removed)>
wrote:

>Both SET and ECHO have problems when the last line contains "<" or ">".


Too true. Other characters can cause problems as well. I doubt it is
possible to have a routine to accurately get the last line of a text
file 100% of the time with only pure batch techniques. Though Herbert
will likely provide a .com creator for this issue (which would be
welcome), 16 bit apps don't work in 64 bit machines, AFAIK.

One can quote the string to handle redirection characters, but that
technique fails if the string has quotes within it. Strings with a
single set of double-quotes in the middle along with redirection
characters are very difficult to handle, such as:

This is > a very " slippery < string.

Ampersands in the string can cause unwanted behavior, if the
sub-string that follows contains valid commands, such as:

This is a test & del *.*

VBscript/WSH may be a better way to tackle this for a built-in
solution. Tom (welcome back), Al and others discussed this recently
in another group.

http://groups.google.com/group/micro...05343ff7cadd12

Cheers,
Clay Calvert
(E-Mail Removed)
Replace "Z" with "L"
 
Reply With Quote
 
Stefan Kanthak
Guest
Posts: n/a
 
      18th Dec 2005
"Gary Smith" <(E-Mail Removed)> wrote:

fup2 microsoft.public.win2000.cmdprompt.admin set, since FOR /F is
available on NT++ only.

> In alt.msdos.batch.nt Harlan Grove <(E-Mail Removed)> wrote:
> > Seems a lot of work just to find the last line. Brute force seems to work
> > for me.

>
> > for /f "delims=" %%s in (%1) do set LASTLINE=%%s
> > echo %LASTLINE%

>
> > Maybe I don't understand the problem. Also, if the set statement above
> > doesn't work, there's always echo %%s > %TEMP%\lastline.tmp, then reading
> > that file's only line into LASTLINE. I don't understand the need to run
> > FIND.

>
> Both SET and ECHO have problems when the last line contains "<" or ">".


And FOR will dissect %1 if it contains blanks, or use it literal when
properly enclosed in quotes.
Unfortunately one can't write "%~1" or '%~1' since both "" and '' have
special meaning with FOR /F.

Strange beast that cat is...
Stefan

 
Reply With Quote
 
Don Grainger
Guest
Posts: n/a
 
      6th Jan 2006
What code would I need to display contents of line x from the bottom? e.g.
the sixth line from the bottom?

Don

"fpschultze" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello.
>
> Here comes a batch file that shows how to read the last line of a text
> file:
>
> =====
> @Echo Off
>
> If %1!==! (
> Echo Read and display the last line of a text file
> Echo.
> Echo Syntax: %~n0 filename.txt
> Goto :EOF)
>
> If Not Exist %1 (
> Echo File not found - %1
> Goto :EOF)
>
> SetLocal
>
> ::String that will be used to identify the last line
> Set
> _=vb043527nc502374v520345c7n20345vb7230457c2n305v72b345v02n3v50237cb230b5v
>
> ::Copy the file to the temp folder
> Copy %1 %temp%.\tmp.tmp >NUL
>
> ::Append unique identifier string to the end of the temp file
> Echo %_% >> %temp%.\tmp.tmp
>
> :etermine the line number of the last line
> For /F "delims=[]" %%a In ('Find /N "%_%" ^< %temp%.\tmp.tmp') Do Set
> _=%%a
>
> ::Set lastline variable to the line next to last line of the original
> file
> Set /A _ -= 2
> For /F "Tokens=* Skip=%_%" %%A In (%1) Do Set lastline=%%a
>
> ::Cleanup
> If Exist %temp%.\tmp.tmp Del %temp%.\tmp.tmp
>
> :isplay line
> Echo %lastline%
>
> EndLocal
> =====
>
> Have fun
>
> --
> Frank-Peter Schultze, http://www.fpschultze.de
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to display first line in a text box TJ Microsoft Excel Programming 3 20th Aug 2009 04:11 PM
How to store/read listbox items in a text file line by line (with line break) ? kimiraikkonen Microsoft VB .NET 6 2nd Nov 2007 05:27 PM
Fast file access reading text file line per line Volker Jobst Microsoft VB .NET 4 23rd Jun 2004 11:23 AM
Reading a large text file line by line backwards Amy L. Microsoft C# .NET 1 2nd Apr 2004 06:03 AM
Reading a large text file line by line backwards Amy L. Microsoft Dot NET 1 2nd Apr 2004 06:03 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:37 PM.