Windows Script Help Urgent..
#1
Scooby Regular
Thread Starter
Join Date: Nov 2001
Location: Leeds - It was 562.4bhp@28psi on Optimax, How much closer to 600 with race fuel and a bigger turbo?
Posts: 15,239
Likes: 0
Received 1 Like
on
1 Post
Is there any quick function for counting the number of lines in a file stream??
I want to read the last four lines from a file.. and at the moment it seems like
lines = 0
open files stream
read file while not eof
temp = readline(Lines)
lines = lines + 1
loop
readline (lines-4)
or similar... seems an **** about tit way so to speak...
David
I want to read the last four lines from a file.. and at the moment it seems like
lines = 0
open files stream
read file while not eof
temp = readline(Lines)
lines = lines + 1
loop
readline (lines-4)
or similar... seems an **** about tit way so to speak...
David
#2
Scooby Regular
Join Date: Jul 2000
Location: Islington
Posts: 2,145
Likes: 0
Received 0 Likes
on
0 Posts
#4
Scooby Regular
Thread Starter
Join Date: Nov 2001
Location: Leeds - It was 562.4bhp@28psi on Optimax, How much closer to 600 with race fuel and a bigger turbo?
Posts: 15,239
Likes: 0
Received 1 Like
on
1 Post
just done it with the following code Ive thrown together..
Dim strFileName
Dim fso
Dim fil
Dim ts
Const ForReading = 1
Const TristateUseDefault = -2
strfilename = "c:\test.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fil = fso.GetFile(strFileName)
Set ts = fil.OpenAsTextStream(ForReading, TristateUseDefault)
ts.ReadAll
wscript.echo ts.Line
ts.Close
Set ts = Nothing
Set fil = Nothing
Set fso = Nothing
Dim strFileName
Dim fso
Dim fil
Dim ts
Const ForReading = 1
Const TristateUseDefault = -2
strfilename = "c:\test.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fil = fso.GetFile(strFileName)
Set ts = fil.OpenAsTextStream(ForReading, TristateUseDefault)
ts.ReadAll
wscript.echo ts.Line
ts.Close
Set ts = Nothing
Set fil = Nothing
Set fso = Nothing
#5
Use a port of the unix command tail available in this archive for which you need a stuffit expander...
Call it from a script if you want
Call it from a script if you want
Trending Topics
#8
Think the quickest way is to do:
set strm = tempFile.OpenAsTextStream(ForReading)
do while ( not strm .AtEndOfStream )
strm .SkipLine
loop
lines = strm.Line
If it's a huge file then it'll take a while. If not... well, it aint pretty but it works
set strm = tempFile.OpenAsTextStream(ForReading)
do while ( not strm .AtEndOfStream )
strm .SkipLine
loop
lines = strm.Line
If it's a huge file then it'll take a while. If not... well, it aint pretty but it works
#9
Scooby Regular
Thread Starter
Join Date: Nov 2001
Location: Leeds - It was 562.4bhp@28psi on Optimax, How much closer to 600 with race fuel and a bigger turbo?
Posts: 15,239
Likes: 0
Received 1 Like
on
1 Post
cheers all.... i ended up with the following...
Option Explicit
' ************************************************** ********************************************
' Declare Variables etc.
' ************************************************** *********************************************
Dim strFileName, fso, fil, ts, LineCount, a, i, objFSO, objFile, objTextFile, CurrLine, Server, strLogName
Dim myArr(4)
Dim ServerList(2) ' Adjust this value for number of servers
Const ForReading = 1
Const TristateUseDefault = -2
' ************************************************** *********************************************
' Set Default Values
' ************************************************** *********************************************
strFileName = "c:\bex10.txt"
ServerList(0) = "CPL2KMBBKP01"
ServerList(1) = "CPLNTMBPRN98"
ServerList(2) = "CPL2KMBDAT01"
For Each Server in ServerList
' ************ To Be Ammended ************
strLogName = "Bex01.txt" ' To be fixed with some logic to work out the latest file.
strfileName = "\\" & Server & "\C$\Program Files\Veritas\Backup Exec\NT\Data\" & strLogName
' ****************************************
' ************************************************** *********************************************
' Create Objects
' ************************************************** *********************************************
Set fso = CreateObject("Scripting.FileSystemObject")
Set fil = fso.GetFile(strFileName)
Set ts = fil.OpenAsTextStream(ForReading, TristateUseDefault)
' ************************************************** *********************************************
' Open File and Read All - Counter will end on last line number read
' ************************************************** *********************************************
ts.ReadAll
Linecount = ts.line
ts.Close
' ************************************************** *********************************************
' Close Resources
' ************************************************** *********************************************
Set ts = nothing
Set fil = Nothing
Set fso = Nothing
' ************************************************** *********************************************
' Read lines from log.
' ************************************************** *********************************************
a = 0 ' Array Counter
i = 5 ' Line Counter
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strFileName)
Set objTextFile = objFile.OpenAsTextStream(ForReading)
Do While objTextFile.AtEndOfStream <> True
CurrLine = objTextFile.Line
If CurrLine = (Linecount-i) Then
myArr(a)=objTextFile.ReadLine
a = a + 1
i = i - 1
Else
CurrLine = objTextFile.ReadLine
End If
Loop
objTextFile.Close
Set objFSO = Nothing
Set objFile = Nothing
' Display Data
Wscript.echo vbcrlf
Wscript.echo "Job Status for Server: \\" & Server & " Reading from logfile: " & strLogName
Wscript.echo myArr(0)
Wscript.echo myArr(1)
Wscript.echo myArr(2)
Wscript.echo myArr(3)
Wscript.echo myArr(4)
Next
Option Explicit
' ************************************************** ********************************************
' Declare Variables etc.
' ************************************************** *********************************************
Dim strFileName, fso, fil, ts, LineCount, a, i, objFSO, objFile, objTextFile, CurrLine, Server, strLogName
Dim myArr(4)
Dim ServerList(2) ' Adjust this value for number of servers
Const ForReading = 1
Const TristateUseDefault = -2
' ************************************************** *********************************************
' Set Default Values
' ************************************************** *********************************************
strFileName = "c:\bex10.txt"
ServerList(0) = "CPL2KMBBKP01"
ServerList(1) = "CPLNTMBPRN98"
ServerList(2) = "CPL2KMBDAT01"
For Each Server in ServerList
' ************ To Be Ammended ************
strLogName = "Bex01.txt" ' To be fixed with some logic to work out the latest file.
strfileName = "\\" & Server & "\C$\Program Files\Veritas\Backup Exec\NT\Data\" & strLogName
' ****************************************
' ************************************************** *********************************************
' Create Objects
' ************************************************** *********************************************
Set fso = CreateObject("Scripting.FileSystemObject")
Set fil = fso.GetFile(strFileName)
Set ts = fil.OpenAsTextStream(ForReading, TristateUseDefault)
' ************************************************** *********************************************
' Open File and Read All - Counter will end on last line number read
' ************************************************** *********************************************
ts.ReadAll
Linecount = ts.line
ts.Close
' ************************************************** *********************************************
' Close Resources
' ************************************************** *********************************************
Set ts = nothing
Set fil = Nothing
Set fso = Nothing
' ************************************************** *********************************************
' Read lines from log.
' ************************************************** *********************************************
a = 0 ' Array Counter
i = 5 ' Line Counter
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strFileName)
Set objTextFile = objFile.OpenAsTextStream(ForReading)
Do While objTextFile.AtEndOfStream <> True
CurrLine = objTextFile.Line
If CurrLine = (Linecount-i) Then
myArr(a)=objTextFile.ReadLine
a = a + 1
i = i - 1
Else
CurrLine = objTextFile.ReadLine
End If
Loop
objTextFile.Close
Set objFSO = Nothing
Set objFile = Nothing
' Display Data
Wscript.echo vbcrlf
Wscript.echo "Job Status for Server: \\" & Server & " Reading from logfile: " & strLogName
Wscript.echo myArr(0)
Wscript.echo myArr(1)
Wscript.echo myArr(2)
Wscript.echo myArr(3)
Wscript.echo myArr(4)
Next
#11
Scooby Regular
I don't know VB personally but the principles are the same; if the lines are fixed length you can (or should be able to) use some kind of seek function, otherwise you don't have any choice but to read all of the file from the beginning, maybe just keeping 4 lines in a FIFO buffer so you don't store the entire file in RAM.
[Edited by stevencotton - 1/7/2003 1:16:20 PM]
[Edited by stevencotton - 1/7/2003 1:16:20 PM]
Thread
Thread Starter
Forum
Replies
Last Post
Mattybr5@MB Developments
Full Cars Breaking For Spares
12
18 November 2015 07:03 AM
hardcoreimpreza
Computer & Technology Related
21
11 October 2015 03:40 PM