Reading next line from data file VB Script

2019-05-25 00:03发布

问题:

I am trying to get the next line of data tag file, however because there are multiple lines with the same tag it is going through and taking the last one but I need the first one. Example tag file below.

Summary SA2100|7775555
Summary SUM100|9674555
Summary SUM100|8544555
Summary SUM100|8555554

From this it is taking 8555554 and not 7775555.

I was trying to put the first line in the if condition and set the string to be the next line is this possible ?

if Left(strLine, 14) = "Summary SA2100" Then
        strSummary = mid(strNextLine, 16, 7)
End If 

Is there a way to use StrNextLine or something similar ?

Thanks

回答1:

Maybe you are referring to something like this:

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\scripts\servers.txt", ForReading)

Do Until objTextFile.AtEndOfStream 
    strNextLine = objTextFile.Readline
    MsgBox strNextLine
Loop

VbsEdit: I don't like to recommend tools, but vbsedit.com has a trial version that keeps partially working with no license. I think it will help you a lot. I personally use it as a light-weight way to quickly check COM object properties and methods.

Sample Library: Moreover, and this is the really important part, they have a large sample library that you can search whenever you need a quick hint on how to do useful stuff. See the sample gallery. The above snippet is adapted from this sample.

There is a menu inside the application that allows quick searching of the whole library, or you can search it online. In the application you search via: Samples => Search Samples.... You get this dialog:


UPDATE:

Change the path to your data file - obviously. In the OpenTextFile method.

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Users\YourName\Desktop\test.txt", ForReading)

Do Until objTextFile.AtEndOfStream 
    strNextLine = objTextFile.ReadLine

    If Left(strNextLine, 14) = "Summary SA2100" Then
        strSummary = mid(strNextLine, 16, 7)
        MsgBox strSummary
        Exit Do
    End If

    'MsgBox strNextLine
Loop


回答2:

Set FSO = CreateObject("Scripting.FileSystemObject")  'create object to open file
Set txtFile = FSO.OpenTextFile("C:\test.txt")   'full path for your file

 Do while Not txtFile.AtEndOfStream 
    strLine = txtFile.ReadLine

    If InStr(1,strLine,"Summary SA2100",1) > 0 Then
      strNextLine=txtFile.ReadLine
        nxtLine= mid(strNextLine, 16, 7)
        Exit Do 
    End If
loop
   MsgBox "The First Line : "& mid(strLine,16,7) & vbcrlf & "The Next Line : " & nxtLine