Reading a certain line of a text file in vbscript

前端 未结 1 1635
孤街浪徒
孤街浪徒 2021-01-27 13:01

Is there a way to read a certain line of a text file using vbscript and storing it as a variable. Thanks.

相关标签:
1条回答
  • 2021-01-27 13:28

    Try this example :

    Option Explicit
    Dim ws,MyFile
    Set ws = CreateObject("WScript.Shell")
    MyFile = "%Windir%\system32\slmgr.vbs"
    MyFile = ws.ExpandEnvironmentStrings(MyFile)
    wscript.echo ExtractLinesFromTextFile(Myfile,1,2) 'Extract line from line 1 to 2
    wscript.echo ExtractLinesFromTextFile(Myfile,2,2) 'Extract line N°2
    wscript.echo ExtractLinesFromTextFile(Myfile,1,5) 'Extract line from line 1 to 5
    wscript.echo ExtractLinesFromTextFile(Myfile,1,10)'Extract line from line 1 to 10
    wscript.echo ExtractLinesFromTextFile(Myfile,8,20)'Extract line from line 8 to 20
    wscript.echo ExtractLinesFromTextFile(Myfile,8,40)'Extract line from line 8 to 40
    wscript.echo ExtractLinesFromTextFile(Myfile,8,50)'Extract line from line 8 to 50
    '*********************************************************************************************************
    Public Function ExtractLinesFromTextFile(ByRef TextFile, ByRef FromLine, ByRef ToLine) '<-- Inclusive
        If FromLine <= ToLine Then
            With CreateObject("Scripting.FileSystemObject").OpenTextFile(TextFile)
                Do Until .Line = FromLine Or .AtEndOfStream
                    .SkipLine
                Loop
                Do Until .Line > ToLine Or .AtEndOfStream
                    ExtractLinesFromTextFile = ExtractLinesFromTextFile & (.ReadLine & vbNewLine)
                Loop
            End With
        Else
            MsgBox "erreur de depassement de lignes", vbCritical, "erreur de depassement de lignes"
        End If
    End Function
    '*********************************************************************************************************
    
    0 讨论(0)
提交回复
热议问题