0

I am new to VBscript and looking for a small help.

There are many log files in a folder. I want to search a string and get its id to a variable.

The log file contains like below line multiple lines.

Line 8123: Feb 01 19:44:40.961 | INFO | JEZHckpTehtA-ADSW4T14T5 | PROD | 10.86.99.108 | Feb 01 2016 19:44:29.595 | EVNT=SWIdmst|DQLN=YN| DQLN=EQUAL_QUAL_QUEUE_GOHEAD_IT

I want to search QUAL_QUEUE_GOHEAD_IT and get its id JEZHckpTehtA-ADSW4T14T5 loaded to a variable and perform some action on it.

Any help is greatly accepted.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Hara
  • 77
  • 1
  • 10
  • This is not how it works on SO: you need to try and show where you stuck. Let's take a look at what you have already achieved. – Leo Chapiro Sep 14 '16 at 14:45
  • I am using InStr function.. If InStr(oFSO.OpenTextFile(strFile).ReadAll, strSearchFor) > 0 Then after this I am struck how to get loaded the value JEZHckpTehtA-ADSW4T14T5 to a variable. – Hara Sep 14 '16 at 14:51
  • You can use Split(YourString, "|") to separate the line into the array of its parts. Now you need to iterate the array with For Each - the question is only how you recognize the desired string? – Leo Chapiro Sep 14 '16 at 14:55

1 Answers1

1

You can use a regular expression. See http://regexr.com/3e7qr for the following one that I have developed.

 INFO \| ([^\|]*) \|.*QUAL_QUEUE_GOHEAD_IT

Using this in vb

    testInput = "Line 8123: Feb 01 19:44:40.961 | INFO | JEZHckpTehtA-ADSW4T14T5 | PROD | 10.86.99.108 | Feb 01 2016 19:44:29.595 | EVNT=SWIdmst|DQLN=YN| DQLN=EQUAL_QUAL_QUEUE_GOHEAD_IT" & vbcrlf & _
    "Line 8123: Feb 01 19:44:40.961 | INFO | JEZHckpTehtA-ADSxxxT5 | PROD | 10.86.99.108 | Feb 01 2016 19:44:29.595 | EVNT=SWIdmst|DQLN=YN| DQLN=EQUAL_QUAL_QUEUE_GOHEAD_IT"

    Set re = New RegExp
    re.Pattern = "INFO \| ([^\|]*) \|.*QUAL_QUEUE_GOHEAD_IT"
    re.Global = True
    Set matches = re.Execute(testInput)
    If matches.Count > 0 Then
        msg = "Found " & matches.Count & " matches:" & vbCRLF
        For Each match In Matches
            msg = msg & "Found match """ & match.SubMatches(0) & vbcrlf
        Next
        msgbox msg, 0, "VBScript Regular Expression Tester"
    Else
        msgbox "No match", 0, "VBScript Regular Expression Tester"
    End Ifs

--- update with file reading ----

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\temp\batches"
Set objFolder = objFSO.GetFolder(objStartFolder)

For Each objFile in objFolder.Files

        set oStream = objFile.OpenAsTextStream(1)
        If not oStream.AtEndOfStream Then
                contents = oStream.ReadAll
        End If
        oStream.Close

        Set re = New RegExp
        re.Pattern = "INFO \| ([^\|]*) \|.*QUAL_QUEUE_GOHEAD_IT"
        re.Global = True
        Set matches = re.Execute(contents)

        For Each match In Matches
                varid = match.SubMatches(0)
                ProcessMatch objFile.Path, varid
        Next

Next

sub ProcessMatch(path, id)
    Msgbox "Match " & id & " found in "  & path
end sub
FloatingKiwi
  • 4,408
  • 1
  • 17
  • 41
  • Thank you sir for the above code this is what I wanted, I want to set a variable with the value of match.SubMatches(0). can you please help me with it. – Hara Sep 15 '16 at 12:56
  • Simply do `myVar = match.SubMatches(0)` – FloatingKiwi Sep 15 '16 at 13:05
  • Thank you again sir, I am trying to put the log file where i need to search it name like this testInput = "C:\Users\hara\Desktop\lota\orene-0102.log" & vbcrlf but it is saying as nomatch found, any suggestions. – Hara Sep 15 '16 at 13:26
  • You aren't searching the file. You are searching the file path. What you need to do is load the contents of your log file into the testInput variable. See http://stackoverflow.com/questions/3117121/reading-and-writing-value-from-a-textfile-by-using-vbscript-code – FloatingKiwi Sep 15 '16 at 13:29
  • Hi sir, my requirement is search QUAL_QUEUE_GOHEAD_IT string in a folder C:\Users\hara\Desktop\lota\ with many log files in it and get its id JEZHckpTehtA-ADSW4T14T5 loaded to another variable and perform some more actions on it. i have pasted the code please help me where to make changes. – Hara Sep 15 '16 at 14:34
  • Hi Sir, I am reading a file testt1.txt please see the code below. output is no match, but the textfile contains the string QUAL_QUEUE_GOHEAD_IT, can you correct me where I am wrong. – Hara Sep 18 '16 at 14:32
  • is it possible to include regexp and instr both in the same code – Hara Sep 20 '16 at 10:47
  • I have written code with Instr function which is doing my job, please see the code i have pasted i have some comments on using regexp in VBscript, Can you pls help me. – Hara Sep 20 '16 at 11:40
  • Don't edit answers and add other questions to them. It makes the post impossible for other readers to follow. You can use regexp and instr in the same method however I cant see a reason for doing it. Looking at your edit you need to get to grips with basic scripting, debugging your script and how regexs work. The answer above points you in the right direction, but I'm not going to code this for you. – FloatingKiwi Sep 20 '16 at 12:39