0

I have a text file in the following format with more than 100 entries

Header Responses 
--- AMD 6001 ---
Sent Packet
FRO 101
A_TEST 5001
Status 0

--- AMD 6002 ---
Sent Packet
FRO 101
A_TEST 5002
Status 1

My requirement is to validate whether the value of 'Status' is 0, if so, proceed further to fetch the random 'A_TEST' value from each entry. Could you please help me with an effective approach to find the 'A_Test' values in each entry, because time taken for execution is more important due to large number entries.

I have written a code in following structure

set txtfile = fso.OpenTextFile("")
set content= txtfile.ReadAll
arrEntry = split (content,"--- AMD")
For num=1 to ubound(arrEntry)
    '2nd entry fails due to its entry status    
    If Instr(arrEntry(num),"Status 0") > 0 Then
        ' How to proceed further to get the value of 'A_TEST' from this array ???? 
    Else
        'Fail - Dont Proceed Further
    End If
Next

1 Answer 1

1

Your code fails, because

set content= txtfile.ReadAll

wrongly uses Set to assign a non-object (String) to a variable. Something like

  Dim sAll : sAll = goFS.OpenTextFile("..\data\31429305.txt").ReadAll()
  Dim s
  For Each s In Split(sAll, "--- AMD")
      If InStr(s, "Status 0") Then
         WScript.Echo Split(s, vbCrLf)(3)
      End If
  Next

will 'work' if you are lucky (EOL marker is vbCrLf, A_TEST always in 4th line).

I'd use a RegExp, as in:

  Dim sAll : sAll     = goFS.OpenTextFile("..\data\31429305.txt").ReadAll()
  Dim rCut : Set rCut = New RegExp
  rCut.Global    = True
  rCut.Multiline = True
  rCut.Pattern   = "^A_TEST (\d+)\r$\n^Status 0\r$"
  Dim m
  For Each m In rCut.Execute(sAll)
      WScript.Echo m.Submatches(0)
  Next
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.