Search code examples
vbscriptscriptingqtphp-uft

Search first occurance of "OK" in a text file and extract first 2 characters in the same line to save it as a variable in UFT/VB scripting


I've been trying to find a proper solution to my problem for several days now looking everywhere. Hopefully some of you guys can direct me to the right direction.

I need to find the string "OK" in a text file and Extract first 2 characters in the same line if I find "OK" to save it as a variable.

I give you an example of the lines you can find in this text file:

Debugger
--------------
>h state 2
Health thread state is: POLLING
Health Devices:
Sensor         Name         State    Eval RED Value ( D , M ) Link  Active Grp    Description
11 ( 2)    TEMP  (  1)        OK      1    1    21  (  1,  0) 0xff   0000   0      01-Inlet Ambient (X:1 y:1)
12 ( 2)    TEMP  (  1)        OK      0    1    40  (  1,  0) 0xff   0000   0      02-CPU 1 (X:11 y:5)
13 ( 2)    TEMP  (  2)     MISSING    0    1     0  (  0,  1) 0xff   0000   0      04-P1 DIMM 1-6 (X:14 y:5)
14 ( 2)    TEMP  (  1)        OK      0    1    24  (  1,  0) 0xff   r0000   0      05-P1 DIMM 7-12 (X:9 y:5)
15 ( 2)    TEMP  (  2)     MISSING    0    1     0  (  0,  1) 0xff   0000   0      06-P2 DIMM 1-6 (X:6 y:5)
16 ( 2)    TEMP  (  2)     MISSING    0    1     0  (  0,  0) 0xff   0000   0      07-P2 DIMM 7-12 (X:1 y:5)
17 ( 2)    TEMP  (  1)        OK      0    1    35  (  1,  0) 0xff   0000   0      08-HD Max (X:2 y:3)
18 ( 2)    TEMP  (  1)        OK      0    1    38  (  1,  0) 0xff   0000   0      10-Chipset (X:13 y:10)
19 ( 2)    TEMP  (  1)        OK      0    1    24  (  1,  0) 0xff   0000   0      11-PS 1 Inlet (X:1 y:14)
20 ( 2)    TEMP  (  2)     MISSING    0    1     0  (  0,  0) 0xff   0000   0      12-PS 2 Inlet (X:4 y:14)
21 ( 2)    TEMP  (  1)        OK      0    1    32  (  1,  0) 0xff   0000   0      13-VR P1 (X:10 y:1)
22 ( 2)    TEMP  (  1)        OK      0    1    28  (  1,  0) 0xff   0000   0      15-VR P1 Mem (X:13 y:1)
23 ( 2)    TEMP  (  1)        OK      0    1    27  (  1,  0) 0xff   0000   0      16-VR P1 Mem (X:9 y:1)
24 ( 2)    TEMP  (  1)        OK      0    1    40  (  1,  0) 0xff   0000   0      19-PS 1 Internal (X:8 y:1)
25 ( 2)    TEMP  (  2)     MISSING    0    1     0  (  0,  0) 0xff   0000   0      20-PS 2 Internal (X:1 y:8)
26 ( 2)    TEMP  (  2)     MISSING    0    1     0  (  0,  0) 0xff   0000   0      21-PCI 1 (X:5 y:12)
27 ( 2)    TEMP  (  2)     MISSING    0    1     0  (  0,  0) 0xff   0000   0      22-PCI 2 (X:11 y:12)
28 ( 2)    TEMP  (  2)     MISSING    0    1     0  (  0,  0) 0xff   0000   0     

Using the following Code I can extract the first 2 characters in a line but I have to extract 2 characters from the line where I find the first occurrence of "OK"

strLine = objTextFile.ReadLine
objTextFile.Close

'Gets first 2 chars 
SerNum = Left(strLine, 2)

Looking for help in this... Thanks in advance...

My unfinished vbscript:

Const ForReading = 1
Dim strSearchFor
strSearchFor = "OK"
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objTextFile = objFSO.OpenTextFile("C:\myFile.txt", ForReading)
		For i = 0 to 20
			strLine = objTextFile.ReadLine()
            If InStr(strLine, strSearchFor) > 0 Then
                SensorNumb = Left(strLine, 2)
            	Exit For
            End If
    	Next

Final Code :

Const ForReading = 1
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objTextFile = objFSO.OpenTextFile("C:\myFile.txt", ForReading)
		For i = 0 to 20
			strLine = objTextFile.ReadLine()
            If InStr(strLine, "OK") > 0 Then
                SensorNumb = Left(strLine, 2)
                objTextFile.Close
            	Exit For
            End If
    	Next


Solution

  • Basically what you want to do is:

    1. read the file line by line
    2. Find a substring using InStr
    3. Print the first two chars Mid(str, 1, 2)

    You should be able to chain these together yourself.