1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Where search match found write following line as well

Discussion in 'Software Development' started by JuniperGreen, Jan 23, 2013.

Thread Status:
Not open for further replies.
  1. JuniperGreen

    JuniperGreen Thread Starter

    Joined:
    Jan 3, 2013
    Messages:
    25
    I have a file which holds lines of text. One line of text indicates a file's location the following line holds a description of the file. I want to be able to search the file using "the directory" as the keyword and where a match is found extract the following line also so I have both the file location and description. For example by searching for one of the directories such as "/s/" I would be able to identify all the files which are in it along with their descriptions. The script below lets me identify all the directory matches but I am stumped as to how I can extract the line following the match so that I have the description as well. There is no way I can search for the descriptions as they are unique.

    Any help on how I might achieve this would be appreciated

    Thank you


    Code:
    Const ForReading = 1
    
    Set objRegEx = CreateObject("VBScript.RegExp")
    objRegEx.Pattern = "./s/"
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("F:\H&S_Safety_Matters\H&S_Items.txt", ForReading)
    
    Do Until objFile.AtEndOfStream
        strSearchString = objFile.ReadLine
        Set colMatches = objRegEx.Execute(strSearchString)  
        If colMatches.Count > 0 Then
            For Each strMatch in colMatches   
                Wscript.Echo strSearchString 
           
    Next
        End If
    Loop
    
    objFile.Close
    
     
  2. draceplace

    draceplace

    Joined:
    Jun 8, 2001
    Messages:
    2,583
    Read\write the next line inside the IF statement. Also not sure the RegEx business is necessary.

    Do Until objFile.AtEndOfStream
    strSearchString = objFile.ReadLine
    IF InString(strSearchString,"./s/") > 0 THEN
    Wscript.Echo strSearchString 'shows match
    strSearchString = objFile.ReadLine
    Wscript.Echo strSearchString 'shows next line
    End IF
    Loop
     
  3. draceplace

    draceplace

    Joined:
    Jun 8, 2001
    Messages:
    2,583
    This might be more what you want..
    Code:
    Do Until objFile.AtEndOfStream
    strSearchString = objFile.ReadLine
    IF InString(strSearchString,"./s/") > 0 THEN
      strDisplay = "Match: " & strSearchString & VbCrLf   
      strSearchString = objFile.ReadLine
      strDisplay = strDisplay & "Discription: " & strSearchString
      Wscript.Echo strDisplay
    End IF
    Loop
    
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1086504

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice