Where search match found write following line as well

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
 

My Computer

Instead of getting all the matches at once you need to read the file line by line, test for a match on the "key lines" as in every other line, when found read in the next line as description. I'm not well grounded in VBScript. But in other scripting languages that can use Scripting.Dictionary object it's possible to use the search string as key and make an array of all values that have that key as the associated item in the assoc array. On the idea key = "search string" item = array[desc1,desc2,desc3] dict(key) = item etc..

There must be examples using VBScript. Just search for a VBScript source repository. PlanetSourceCode is one. I'm not sure if it has VBS as well as Visual Basic. But there must be dozens out there.

Also check the ms documentation using scripting.dictionary for all the properties and methods.

edit: I have a file comment utility called Comment Explorer. It includes ahk source code. It only has one comment per file. But it may give you some idea how scripting.dictionary may be used. You may download from this page
 
Last edited:

My Computer

System One

  • Manufacturer/Model
    HP Pavilion m9515y
    CPU
    Phenom X4 9850
    Memory
    8 GB
    Graphics Card(s)
    Some Radeon Cheapie with 512 MB Ram
    Monitor(s) Displays
    CRT
    Screen Resolution
    1280x1024
    Hard Drives
    750 GB SATA 3G
    2 SIIG Superspeed docks w/WD Caviar Black Sata II or III
Back
Top