Timo Salmi
2005-05-08 05:09:19 UTC
The batch code below does the same and requires WSH which ships
with all flavours of Windows since Windows 98 (or SE). :: Create a
temp. VBS file that shows the last 10 lines of a text file
ECHO content = Split(file.ReadAll, vbCrLf, -1, 1)>>%TEMP%.\_.VBS
Scripting Runtime Library documentation: "For large files,with all flavours of Windows since Windows 98 (or SE). :: Create a
temp. VBS file that shows the last 10 lines of a text file
ECHO content = Split(file.ReadAll, vbCrLf, -1, 1)>>%TEMP%.\_.VBS
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:***@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:***@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip