0

I need to find this in a text file

Property Name="Manufacturer" Value="LENOVO"/>

Result that I want output is something like this Manufacturer= LENOVO

I've tried using examples from both Split and InStr but the issue I am having are all the Quotes in the string. A little help would be appreciated. If I could just get the basics of this I plan to put it in an array to find other things in the text file.

Ken White
  • 123,280
  • 14
  • 225
  • 444
TomC
  • 11
  • 1
    Looks suspiciously like XML to me, in which case you're probably better off using XPath. – Rno Jul 06 '20 at 19:31
  • Here is a snip it in the text file. – TomC Jul 06 '20 at 19:33
  • @TomC Take a look at [Navigating XML nodes in VBScript, for a Dummy](https://stackoverflow.com/questions/9723903/navigating-xml-nodes-in-vbscript-for-a-dummy) – Hackoo Jul 07 '20 at 07:24
  • Does this answer your question? [Navigating XML nodes in VBScript, for a Dummy](https://stackoverflow.com/q/9723903) – user692942 Jul 07 '20 at 08:32

1 Answers1

0

Refer to this answer here Navigating XML nodes in VBScript, for a Dummy and assume that you have an xml file stored in this location "C:\Temp\Test.xml"

Test.xml

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <Property Name="Manufacturer" Value="LENOVO" />
   <Property Name="Model" Value="10RS0014US" />
   <Property Name="Domain" Value="Domain.US" />
   <Property Name="DomainRole" Value="1" />
   <Property Name="NumberOfProcessors" Value="1" />
   <Property Name="NumberOfLogicalProcessors" Value="6" />
   <Property Name="TotalPhysicalMemory" Value="8417189888" />
   <Property Name="Status" Value="OK" />
   <Property Name="UserName" Value="Domain\User" />
</root>

Vbscript

Option Explicit
Dim Title,objDoc,objRoot,child,Name,Value,objNode,Manufacturer_Value,Name_Value
Title = "Navigating XML nodes in VBScript"
Set objDoc = CreateObject("MSXML.DOMDocument")
objDoc.Load "C:\Temp\Test.xml"

' Iterate over all elements contained in the <root> element:
Set objRoot = objDoc.documentElement
Name = ""
Value = ""

For Each child in objRoot.childNodes
   Name = Name & child.getAttribute("Name") & vbcrlf
   Value = Value & child.getAttribute("Value") & vbcrlf
   Name_Value = Name_Value & child.getAttribute("Name") & " = "& child.getAttribute("Value") & vbcrlf
Next

MsgBox Name,vbInformation,Title
MsgBox Value,vbInformation,Title
MsgBox Name_Value,vbInformation,Title

'Find a particular element using XPath:
Set objNode = objDoc.selectSingleNode("/root/Property[@Name='Manufacturer']")
Manufacturer_Value = objNode.getAttribute("Value")
MsgBox "Manufacturer = " & Manufacturer_Value,vbInformation,Title

Other method using RegEX :

Option Explicit
Dim Title,fso,s,r,m,Manufacturer_Value
Title = "Extract Value with RegEx in Vbscript"
Set fso = CreateObject("Scripting.FileSystemObject")
s = fso.OpenTextFile("C:\Temp\Test.xml").ReadAll()
Set r = New RegExp
r.Global = False
r.Pattern = "Value=\x22(\S+)\x22"

For Each m In r.Execute(s)
    Manufacturer_Value = m.Submatches(0)
Next

MsgBox "Manufacturer = "& Manufacturer_Value,vbInformation,Title

3rd Method : Read file and search for string using Instr and Split

Option Explicit
Dim Title,FSO, ReadTextFile
Dim Textline,Manufacturer,Manufacturer_Value
Title = "Read file and search for string using Instr and Split"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set ReadTextFile = FSO.OpenTextFile("C:\Temp\Test.xml", 1) ' Open the file with read only mode

Do Until ReadTextFile.AtEndOfStream
    Textline = ReadTextFile.Readline()
    If Instr(Lcase(Textline), Lcase("Manufacturer")) Then ' If textline contain string "Manufacturer"
        Manufacturer = Split(Textline,chr(34))(1) ' Split the textline  with Double quote chr(34) indicator
        Manufacturer_Value = Split(Textline,chr(34))(3)
        Exit Do ' If we find our string we can leave the Do ..Loop
    End If
Loop ' Read through every line

Msgbox Manufacturer & " = " & Manufacturer_Value,vbInformation,Title
Hackoo
  • 18,337
  • 3
  • 40
  • 70