Vbscript to read text line in a file into a variable

February 10, 2020 at 10:30:41
Specs: Windows 7, 1.6 gb 4gb
HI, I'm embarrassed to admit that I'm stumped here. I need vbscript to read in the 2nd the last line in a file into a variable. The number of lines in the file is unpredictable and subject to change which makes it awkward. I have searched but couldn't find anything. There is lots about batch files and the first line or the last line or a specific line number but of no help. I wouldn't know the line number because of the varying number of lines and I need it to be vbscript. If anyone can give me a url or some lines of script I would be grateful.

See More: Vbscript to read text line in a file into a variable

Reply ↓  Report •

#1
February 10, 2020 at 21:09:09
I think you want "instrrev" function. Here's a "simple sample", saved as 'juniper.vbs':

set fso=createobject("scripting.filesystemobject")
a=fso.opentextfile("juniper.vbs",1).readall
lines=2
c=0
p2=-1
do while c<=lines
p=instrrev(a,vbcrlf,p2)
p1=p2
p2=p
c=c+1
loop
wscript.echo mid(a,p2+2,p1-p2-2)
wscript.quit
'----------- end script
Of course, vbcrlf might be any other newline chars other than "standard" crlf
fixed my sloppy code, just for reference only... Mainly cause, like you, I hate to leave loose ends and defective code posted here. I just saved the next-to-last crlf into p1 and used that as basis of the number of bytes to deliver off of p2. It was while solving a similar problem a few years back, that I stumbled on instrrev in vbscript help file.

message edited by nbrane


Reply ↓  Report •

#2
February 11, 2020 at 02:35:43
Hi nbrane, thank you for your response. When I ran the script it worked but gave me the last 3 lines of a text file. What I was after was the 2nd the last line only so I could read it into a variable. I probably didn't explain that very well. However, I managed to cobble up some lines to get me what I wanted. Truth to tell it smacks more of Heath Robinson than proper scripting!

It was simple enough. What I did was read the text file to determine the number of lines in it (I'm sure there must be another better way to do this!), then to get the 2nd last line, store the number of lines minus one in a variable. Then I read the text file in again but this time used

For x = 1 to str2ndLastPointer
Textline = ReadTextFile.ReadLine
str2ndLast = TextLine
next

wscript.echo str2ndLast

If you could see your way to modify your script that would be great because I would rather do something properly than get by with something cobbled together. Anyway there's no reason it can't be claimed as a solved answer because I could have read your answer into a string then chopped off the first line and the last line leaving me with your middle line which would have been the 2nd last line I was trying to get.

Many thanks again for your help.


Reply ↓  Report •

#3
February 11, 2020 at 08:51:38
If anyone is interested (probably not!) this is what I added to nbrane's script to get the data in the second last line of the text file into the variable "SecondLastLine":

set fso=createobject("scripting.filesystemobject")
a=fso.opentextfile("YourFile.txt",1).readall
lines=2
c=0
p2=-1
do while c<=lines
p=instrrev(a,vbcrlf,p2)
p2=p
c=c+1
loop
wscript.echo mid(a,p2)
'Addition To Script
a = mid(a,p2)
arr= split(a,vbCrLf)
SecondLastLine = arr(2)
wscript.echo SecondLastLine



Reply ↓  Report •
Related Solutions


Ask Question