Read text file from a symbol up to a symbol.

Discussion in 'Scripting' started by Inetimon, Jan 30, 2007.

  1. Inetimon

    Inetimon Guest

    How to read through the text from a file from a symbol up to a symbol. For
    example from a word "Stat" up to a word "stop". ?
     
    Inetimon, Jan 30, 2007
    #1
    1. Advertisements

  2. Off the top of my head you might use Instr to find Start, Instr to find
    Stop, calculate the difference between the two, then use Mid to pull out
    the text inbetweeen.

    --
    Jeffery Hicks
    SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com
    VBScript & Windows PowerShell Training -
    www.ScriptingTraining.com/classes.asp
    Windows PowerShell? - www.SAPIENPress.com/powershell.asp

    blog: http://blog.SAPIEN.com
    blog: http://jdhitsolutions.blogspot.com
     
    Jeffery Hicks, Jan 30, 2007
    #2
    1. Advertisements

  3. Inetimon

    LJB Guest

    This might do what you want

    MyPattern = "start.*?stop"

    'MyString = CreateObject("Scripting.FileSystemObject") _
    ' .OpenTextFile("C:\test.txt", 1) _
    ' .ReadAll
    MyString = "here is a start string to tostopto test stop stop this on" &
    vbNewLine & "another start and stop location"

    set RegEx = CreateObject("VBScript.RegExp")
    RegEx.IgnoreCase = false
    RegEx.Global = true
    RegEx.Multiline = true

    RegEx.Pattern = MyPattern

    Wscript.Echo "Pattern """ & RegEx.Pattern & """ found " &
    RegEx.Execute(MyString).count & " time(s)"

    Set Matches = RegEx.Execute(MyString)
    For Each Match in Matches
    RetStr = RetStr & "Match found at position " & _
    Match.FirstIndex + 1 & ". Match Value is '" & _
    Match.Value & "'. (" & Match.Length & " characters)" & vbNewLine
    Next
    Wscript.Echo RetStr
     
    LJB, Jan 30, 2007
    #3
  4. Quickest would be to read entire file with ReadAll method of File object,
    then find first instance of "Start", then first instance after that of
    "Stop" (using the InStr function), then parse with Mid function to get the
    text you want. For example (not tested):
    ==========
    Const For Reading = 1

    ' Specify file.
    strFile = "c:\Scripts\MyFile.txt"

    ' Use FileSystemObject to open the file.
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(strFile, ForReading)

    ' Read entire file.
    strText = objFile.ReadAll

    ' Look for first instance of "Start".
    intStart = InStr(strText, "Start")

    ' Look for first instance of "Stop" after "Start".
    intStop = InStr(intStart, strText, "Stop")

    ' Extract relevant text.
    strFound = Mid(strText, intStart + 5, intStop - intStart - 5)
    =======
    If "Start" and "Stop" are always on the same line, I would use the ReadLine
    method rather than ReadAll and process one line at a time in a loop. In
    brief (not tested):
    =========
    Do Until objFile.AtEndOfStream
    strText = objFile.ReadLine
    intStart = InStr(strText, "Start")
    If (intStart > 0) Then
    intStop = InStr(intStart, strText, "Stop")
    strFound = Mid(strText, intStart + 5, intStop - intStart - 5)
    Wscript.Echo strFound
    End If
    Loop
     
    Richard Mueller [MVP], Jan 30, 2007
    #4
  5. I have a natural aversion for calculations of indexes and offsets of
    characters in strings
    because of the likelihood of one-off errorsso I would tend to do it this way
    instead:

    strFound = array(strText & "Start", "Start")(1)
    strFound = array(strFound, "Stop")(0)

    /Al
     
    Al Dunbar [MS-MVP], Jan 31, 2007
    #5
  6. Aaaargh, and thanks for the correction. I am trying to configure
    PrimalScript as the default editor in my newsreader. When that is done, I
    hope it will catch most of my gaffes!

    /Al
     
    Al Dunbar [MS-MVP], Feb 3, 2007
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.