I know I am a spoil-sport, but using 4DOS this is, again, a 1-liner.
To see the last 10 lines of a file, use
tail /n10 myfile.txt
If you want to watch the last 10 lines of myfile.txt continuously, put
the tail command into a loop with DELAY like
do forever
tail /n10 myfile.txt
delay 10
enddo
Of course this batch will runder all versions of DOS, Win)n/ME and
WinNT/XP. All you need to do is download and install 4DOS.
--
* Klaus Meinhard *
www.4dos.info
- 4DOS Info -
- Info for DOS -
Klaus,
you appear to have completely missed the point of the exercise.
This constant advocacy for third-party tools, however free or stable or
slick simply denies the academic challenge of using Bill's Broken Toolkit.
Any problem presented here can be solved by an appropriate assembler
program, or an equivalent in some higher-level language.
Such solutions are ASSEMBLER solutions or WHATEVER-LANGUAGE solutions, not
batch solutions.
Your solution to problems appears to boil down to 'it's easier to use 4Dos',
which makes you sound like the unfortunate Mr. XSET of six or seven years
ago. Your evangelising is tedious. Please have the courtesy to realise that
if dos-batch regulars wanted to use 4Dos, they would - and their solutions
would then be posted in comp.os.msdos.4dos or some similar 4dos discussion
group.
...Bill
> you appear to have completely missed the point of the exercise.
> This constant advocacy for third-party tools, however free or stable
> or slick simply denies the academic challenge of using Bill's Broken
> Toolkit.
Oh, I know that :-)
> Any problem presented here can be solved by an appropriate
> assembler program, or an equivalent in some higher-level language.
> Such solutions are ASSEMBLER solutions or WHATEVER-LANGUAGE
> solutions, not batch solutions.
We are on a slippery slope here: many "batch" solutions of nontrivial
problems apply 3rd-party tools, from little one-off assembler solutions
via grown-up helpers like sed and awk to complete languages like basic
or pascal. My (tiresome) argument is that you don't need so many tools.
> Your solution to problems appears to boil down to 'it's easier to use
> 4Dos', which makes you sound like the unfortunate Mr. XSET of six or
> seven years ago. Your evangelising is tedious.
I may have overdone it a bit :-)
But you should realize that these 2 problems (tail and converting hex to
dec) are real problems even in the enhanced XP batch world, requiring an
certain amount of nontrivial code. They boil down to trivial solutions
when you use 4DOS.
The point is: this newsgroup is for 2 groups, a guru group of regulars
which enjoys working command.com's batch abilities and bugs through the
paces, _and_ people seeking workable solutions (they mostly don't care
about batch one way or another, they just want a solution and try their
luck in amb). For the first group the mentioning of 4DOS solutions is a
constant spoil-sport (understandably). For the second it is a hint that
there is a tool out there to enhance their productivity tenfold.
I have already written at length about the (lack of) ability of
command.com batches as sometimes produced in this group to function in a
professional environment due to their lack of maintainability, language
independancy, version independancy and adaptability, so I won't start
this again :-)
> Please have the
> courtesy to realise that if dos-batch regulars wanted to use 4Dos,
> they would - and their solutions would then be posted in
> comp.os.msdos.4dos or some similar 4dos discussion group.
I will tone it down a bit :-) , but I guess from time to time 4DOS will
get the better of me, especially when I think it will do some good :-))
=====
:: Create a temp. VBS file that shows the last 10 lines of a text file
ECHO Set fso =
WScript.CreateObject("Scripting.FileSystemObject")>%TEMP%.\_.VBS
ECHO Do While fso.FileExists(WScript.Arguments(0))>>%TEMP%.\_.VBS
ECHO Set file = fso.OpenTextFile(WScript.Arguments(0),
1)>>%TEMP%.\_.VBS
ECHO content = Split(file.ReadAll, vbCrLf, -1, 1)>>%TEMP%.\_.VBS
ECHO For i = UBound(content)-10 To UBound(content)>>%TEMP%.\_.VBS
ECHO WScript.Echo content(i)>>%TEMP%.\_.VBS
ECHO Next>>%TEMP%.\_.VBS
ECHO Set file = Nothing>>%TEMP%.\_.VBS
ECHO WScript.Sleep 10000>>%TEMP%.\_.VBS
ECHO Loop>>%TEMP%.\_.VBS
:: Call the temp. VBS file for myfile.txt.
CSCRIPT //Nologo %TEMP%.\_.VBS myfile.txt
:: Next line will be executed if another process has deleted my
file.txt
IF EXIST %TEMP%.\_.VBS DEL %TEMP%.\_.VBS
=====
Regards
Frank-Peter Schultze [http://www.fpschultze.de]
> ECHO content = Split(file.ReadAll, vbCrLf, -1, 1)>>%TEMP%.\_.VBS
Scripting Runtime Library documentation: "For large files,
using the ReadAll method wastes memory resources. Other techniques
should be used to input a file, such as reading a file line by line."
Here is an alternative, written for XP
A Visual Basic Script (VBScript) aided command line script version
of "tail"
@echo off & setlocal enableextensions
::
:: Make a test file
for %%i in (1 2 3 4 5 6) do echo This is line %%i>>MyFile.txt
echo.>>MyFile.txt
for %%i in (8 9 10 11 12) do echo This is line %%i>>MyFile.txt
::
:: Build a Visual Basic Script
set skip=
set vbs_=%temp%\vtail.vbs
findstr "'%skip%VBS" "%~f0" > %vbs_%
::
:: Run the script with Microsoft Windows Script Host Version 5.6
cscript //nologo %vbs_% "MyFile.txt" 3
::
:: Clean up
for %%f in (%vbs_% MyFile.txt) do del %%f
endlocal & goto :EOF
'
'.......................................................
'The Visual Basic Script
'
' Define some constant
Const ForReading = 1, ForWriting = 2, ForAppending = 8 'VBS
Dim fileName, FSO, f, i, n, nArg, s 'VBS
'
' Check the right usage
nArg = WScript.Arguments.Count 'VBS
If (nArg = 0) Or (nArg > 2) Then 'VBS
s = "Usage: cscript //nologo " 'VBS
s = s & WScript.ScriptName 'VBS
s = s & " fileName [NumberOfLinesFromEnd]" 'VBS
WScript.Echo s 'VBS
WScript.Quit 'VBS
End If 'VBS
'
' Get the arguments, fileName and tailLines
fileName=WScript.Arguments.Unnamed(0) 'VBS
If WScript.Arguments.Count > 1 Then 'VBS
tailLines = CLng(WScript.Arguments.Unnamed(1)) 'VBS
Else 'VBS
tailLines = 10 'VBS
End If 'VBS
'
' Check that the file exists
Set FSO=CreateObject("Scripting.FileSystemObject") 'VBS
if not FSO.FileExists(fileName) Then 'VBS
WScript.Echo "File " & fileName & " Not Found" 'VBS
WScript.Quit 'VBS
End If 'VBS
'
' Calculate the number of lines
Set f = FSO.OpenTextFile(fileName, ForReading, True) 'VBS
n = 0 'VBS
Do While Not f.AtEndOfStream 'VBS
n = n + 1 'VBS
s = f.ReadLine 'VBS
Loop 'VBS
'
' Output the tail part of the file
Set f = FSO.OpenTextFile(fileName, ForReading, True) 'VBS
i = 0 'VBS
Do While Not f.AtEndOfStream 'VBS
i = i + 1 'VBS
s = f.ReadLine 'VBS
If i > n - tailLines Then WScript.Echo s 'VBS
Loop 'VBS
The output will be
C:\_D\TEMP>cmdfaq
This is line 10
This is line 11
This is line 12
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
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip
Assuming that the file consists of a large number of lines, but
that the number of tail lines to be output is relatively small, it's
more efficient to read the file once only. During the read, instead
of counting the lines, you push each line into a small, rotating
stack held in a (size=TailLines) dynamic array.
At the EndOfStream, you pop the stack array to echo the tail.
Leaving out the checking routines for arguments/file-existence,
this fragment of VBS code shows the rotating stack algorithm
(assumes filename=first argument TailLines=second argument):
Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
Set Args=WScript.Arguments
fileName=Args(0):tail=Args(1)
Set fso=CreateObject("Scripting.FileSystemObject")
Set f=fso.OpenTextFile(fileName, 1)
n=0:ReDim line(tail)
Do While Not f.AtEndOfStream
n=(n+1) Mod tail
line(n)=f.ReadLine
Loop
For j=1 To tail
n=(n+1) Mod tail
wscript.echo line(n)
Next
====End cut-and-paste (omit this line)
For study/demo use. Cut-and-paste as VBScript .VBS plain-text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects
============Screen capture Windows 95
C:\WORK>type file.txt
one
two
three
four
five
six
seven
eight
nine
ten
C:\WORK>cscript//nologo demo.vbs file.txt 4
seven
eight
nine
ten
C:\WORK>
============End screen capture
--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
Header email is rarely checked. Contact us at http://www.allenware.com/
Nice code, but it needs to verify that 'tail' has been reached rather than
assume the file has more lines then the number provided. The following
condition will satisfy that need.
If Not line(n)=vbEmpty Then wscript.echo line(n)
--
Todd Vargo (double "L" to reply by email)
It's a one-liner when implemented using SED, too.
set N=10
set FILE=datafile.txt
sed ":b;$q;N;s/\n/&/%N%;ta;bb;:a;D" %FILE%
Not lightning fast, but does the job for returning up to 80 or so lines.
Standard Caveat
Under DOS, users must test every SED script individually to make sure it
is going to work with their particular executable. Every DOS version of
SED I have tried contains one or more bugs which will inevitably disallow
correct operation of at least some scripts. This TAIL script executes
fine with sed302 but not with sed118 or sed15.
--
John Savage (my news address is not valid for email)
Then use MiniTrue, which has had a single author and a single line of
development. HEAD is Example 6 in the Help, and TAIL is an appendage to
HEAD.
It's more convenient for file editing that SED is, and also does stream
editing and file viewing (Hex optional).
ISTR that it's in ftp://garbo.uwasa.fi/pc/fileutil/ as well.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt;
free, DOS/Win/UNIX, <URL:http://www.idiotsdelight.net/minitrue/> Update hope?