computing
  • 6

Vbscript To Read Text Line In a File Into a Variable

  • 6

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.

Share

1 Answer

  1. 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

    • 0