I'm trying to create an environment variable by parsing a very ugly XML file returned by a curl command. Contained in the XML is a MAC address that I'm trying to use FINDSTR to find and then SET it as an environment variable for the local user.

Here is a snippet of the XML that the curl command returns:

<CameraStatusModel xmlns="***URL contains sensitive info, so removed it***"> <CameraStatusModel.ActiveCamera> <RectilinearCameraDeviceModel CompassHeading="0" IsConfigured="False" MACAddress="00:30:53:27:27:f4" Make="Basler" Name=""> <RectilinearCameraDeviceModel.CalibrationMask> <RectilinearCalibrationMaskModel Id="a1155f60-2900-442c-82a8-6c00dadbf243" Length="0" Width="0" ></RectilinearCalibrationMaskModel>

I think the FINDSTR command I need to use is: FINDSTR /r [a-z0-9]{2}:[a-z0-9]{2}:[a-z0-9]{2}:[a-z0-9]{2}:[a-z0-9]{2}:[a-z0-9]{2} c:\Users\<MyUsername>\Desktop\sandbox\output.txt. I also know that I can use SET cameraMAC=<something> to create the environment variable, which I can confirm using ECHO %cameraMAC%. What I don't know is how I can get the FINDSTR to be assigned to the SET command.

Any suggestions would be appreciated. Thank you.

I've never really liked FINDSTR. For one thing, FINDSTR won't allow you to parse out the mac address. It will just list lines containing a matched string. I tend to do this sort of thing with vbScript. Here's how I did it. Copy the following code into a text file named GetMacAddr.vbs

set rex = New RegExp
    rex.Pattern = rex.Pattern = "(([\dA-F]{2}:){5}[\dA-F]{2})"
    rex.IgnoreCase = True

xml = Wscript.Stdin.ReadAll

if rex.Test(xml) Then
    mac = rex.Execute(xml)(0).SubMatches(0)
    Wscript.StdOut.WriteLine "set CameraMAC=" & mac
end if

Make sure your default script engine is cscript by opening an admin cmd shell (only need to do this once) and typing

cscript //nologo //h:cscript //s

Then you can extract the mac address from a file by

GetMacAddr <test.xml

substituting your file name for test.xml. The final step is execute the set command returned by GetMacAddr. You do this by piping the output to cmd.exe. The complete line looks like

GetMacAddr <test.xml | cmd

If you close the current shell and open a new one you will see %CameraMAC% defined. It will be in the User rather than System section if you look at Environment variables through the GUI.

As a further note on regular expressions, note that I put ( and ) around the expression I am looking for. This defines a SubMatch expression. Each set of parentheses creates a new SubMatch expression which can be accessed by indexing (zero relative).

commented: Thank you very much. I'll try your script. I appreciate it. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.