OrganizedChaos
OrganizedChaos

Reputation: 451

VBScript return value in string

I am working with a product that uses VBScript extensively and I need to parse a string to extract specific data. I know how to do this with other languages, but need to be pointed in the right direction with VBScript please.

Here's the string I am reading from a text file.

<PGConfiguration GPOName="Production Policy" GPODisplayName="Production Policy" GPOVersion="0" />

The value between GPOName="I_NEED_THIS_DATA" needs to be pulled. It is not expected to be the same between instances.

I have a script that uses InStr() to make sure GPOName= exists and gives me the character position. I've been able to use that with Mid() to start after the GPOName=". I don't know how to find the count or position to the closing quote.

Am I going down the right path with InStr() and Mid(), or is there a better way to approach this (regex maybe??)

This code will open the file, read in the contents and pop up a box with the first 10 characters of what I am trying to get to. Where do I go from here?

Thanks!

Dim objFSO, objTextFile, Stream, strSearchFor, strFileName, strLine, strPosition
' the file to check
strFileName = "C:\checkme.txt"
' what are we looking for
strSearchFor = "GPOName="

' create the object and open the file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set Stream = objFSO.OpenTextFile(strFileName, 1, False)
Set objTextFile = objFSO.OpenTextFile(strFileName, 1, 0)

' read the first line with my data on it
strLine = objTextFile.ReadLine()
' do an InStr to get the position of the data
strPosition = InStr(strLine, strSearchFor)
' set the offset to 9 to get to the end of the ="
strPosition = strPosition + 9
' print of the first 10 characters after the GPOName=" match
wscript.Echo Mid(strLine,strPosition,10)

Upvotes: 0

Views: 2443

Answers (2)

Gurmanjot Singh
Gurmanjot Singh

Reputation: 10360

In addition to the answer provided by Mithilesh, If you want to use Regex, you can try the following:

Dim objFSO, objTextFile, strFileName, strTemp, objReg, objMatches
' the file to check
strFileName = "C:\checkme.txt"

' create the object and open the file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(strFileName, 1, False)

'Read all the data, store in a variable and close the file
strTemp = objTextFile.ReadAll()
objTextFile.Close

'Regex code
Set objReg = New RegExp
objReg.Global=True
objReg.Pattern = "(?:GPOName="")([^""]+)(?="")"     'In vbscript, if we want to have a double quote inside a string, it has to be preceded with another double quote to escape it. If just one double-quote is used, it may indicate end or start of a new string.
If objReg.Test(str) Then
    Set objMatches = objReg.Execute(str)
    objMatches.Item(0).Submatches.Item(0)           'Returns the 1st match; To get all the matches run the loop to objMatches.Count
End If
Set objReg = Nothing
Set objFso = Nothing

REGEX DEMO

Regex explanation: (?:GPOName=")([^"]+?)(?=")

( - Starts a Group

?: - makes the current group a non-capturing group

GPOName=" - Finds the substring - GPOName"

) - Non-Capturing group ends

( - Starts a group which we want to capture. Notice that it is not followed by ?:

[^"] - matches a character which is NOT a double quote

+ - repeats the preceding token 1 or more times greedily(as many times as possible i.e, as long as there is a match)

) - Capturing Group Ends

(?=") - Positive Lookahead. It means that a double quote must follow the preceding string matched/returned by our capturing group. The double quote will not be the part of the match

For learning the Regular expression basics, refer THIS

Upvotes: 1

Mithilesh Indurkar
Mithilesh Indurkar

Reputation: 489

Depends on if you need to achieve this using string or XML, you can use either of them

Set objTextFile = objFSO.OpenTextFile(strFileName, 1, 0)

' read the first line with your data on it
strLine = objTextFile.ReadLine()

arr = split(strLine , "GPOName",2)

arr2 = Split(arr(1),"""")
msgbox arr2(1)

Need to try if you can load your file as an XML or just load the line as XML as seen below. This is a cleaner solution.

Set objXML = CreateObject("msxml2.DOMDOCUMENT")
objXML.loadXML strLine 
msgbox objXML.selectSingleNode("PGConfiguration/@GPOName").text 'Or nodevalue, whichever suites you
Set objXML = Nothing

Upvotes: 1

Related Questions