Home > OS >  Capturing Specific Data using Batch Script
Capturing Specific Data using Batch Script

Time:01-27

Hi so I need help on how I can get specific data from multiple XML files and put it in variable or into one list.

The sample XML is below.

<?xml version="1.0" encoding="utf-8"?>
<Data>
  <DataType>Temptale</DataType>
  <MonitorType>Temptale Ultra</MonitorType>
  <SerialNumber>MAH6P002K0</SerialNumber>
  <NumberOfDataPoints>402</NumberOfDataPoints>
  <FirstActivationTime>1/1/1970 12:00:00 AM</FirstActivationTime>
  <FirstDataPointTime>1/18/2023 12:36:23 AM</FirstDataPointTime>
  <StartTime>1/18/2023 12:36:23 AM</StartTime>
  <StopTime>1/18/2023 3:57:08 AM</StopTime>
  <Configuration>
    <SensitechConfiguration>
      <ProductID>10</ProductID>
      <FirmwareVersion>615</FirmwareVersion>
      <CustomerResets>0</CustomerResets>
      <UserInfoCharEncoding>ASCII</UserInfoCharEncoding>
      <EepromSize>EEPROM_16K</EepromSize>
      <PrimarySensorTypeID>BIO</PrimarySensorTypeID>
      <CommandByte value="01">STOPPED</CommandByte>
      <RadioSupported>false</RadioSupported>
      <BlockDownload>true</BlockDownload>
      <PrimarySensorThermType>13</PrimarySensorThermType>
      <PrimarySensorProbe>false</PrimarySensorProbe>
      <SecondarySensorTypeID>AMBIENT</SecondarySensorTypeID>
      <SecondarySensorThermType>0</SecondarySensorThermType>
      <SecondarySensorProbe>false</SecondarySensorProbe>
      <SecondarySensorEnabled>false</SecondarySensorEnabled>
      <Sensor1CalibrationOffset1>0</Sensor1CalibrationOffset1>
      <Sensor1CalibrationOffset2>0</Sensor1CalibrationOffset2>
      <Sensor1CalibrationOffset3>0</Sensor1CalibrationOffset3>
      <TimeZoneOffset>0</TimeZoneOffset>
      <Add1PercentRH>false</Add1PercentRH>
      <Add2PercentRH>false</Add2PercentRH>
      <TemperatureCompensationDisable>false</TemperatureCompensationDisable>
      <IdealTemperatureRangeEnable>false</IdealTemperatureRangeEnable>
      <PrintCertificateEnable>true</PrintCertificateEnable>
      <LcdHeartbeatIcon>false</LcdHeartbeatIcon>
      <TtvFileCreationDisable>false</TtvFileCreationDisable>
      <TimeZoneShiftEnable>false</TimeZoneShiftEnable>
      <TimeZoneDaylightEnable>false</TimeZoneDaylightEnable>
      <PdfFileCreationOption>CreateFull</PdfFileCreationOption>
      <ConfigurationPasswordEnable>false</ConfigurationPasswordEnable>
      <DownloadPasswordEnable>false</DownloadPasswordEnable>
      <LcdDisplayAlarmNumbers>false</LcdDisplayAlarmNumbers>
      <OkAlarmPrefixEnable>false</OkAlarmPrefixEnable>
      <NormalizedGridlineEnable>false</NormalizedGridlineEnable>
      <PDF_AFormat>false</PDF_AFormat>
      <PdfPasswordEnable>false</PdfPasswordEnable>
      <TtvPasswordEnable>false</TtvPasswordEnable>
      <PdfLanguage>ENGLISH</PdfLanguage>
      <ExpirationDate>1/1/1970 12:00:00 AM</ExpirationDate>
    </SensitechConfiguration>
    <UserConfiguration>
      <MeasurementInterval>30</MeasurementInterval>
      <StartUpDelay>0</StartUpDelay>
      <StopButtonEnable>true</StopButtonEnable>
      <StopButtonDelay>false</StopButtonDelay>
      <MultiDrop>false</MultiDrop>
      <LcdAlarmNotification>true</LcdAlarmNotification>
      <StartButtonDelay>false</StartButtonDelay>
      <SoftwareVersion>6921</SoftwareVersion>
      <TripNumber>0</TripNumber>
      <LcdBlink>false</LcdBlink>
      <LcdTemperatureUnit>Fahrenheit</LcdTemperatureUnit>
      <LcdSummary>true</LcdSummary>
      <CelsiusWith2Decimal>false</CelsiusWith2Decimal>
      <DisplayCurrentTemperature>true</DisplayCurrentTemperature>
      <FlashDriveWriteEnable>true</FlashDriveWriteEnable>
      <LcdContrast>7</LcdContrast>
      <LcdStartupDelayIconEnable>false</LcdStartupDelayIconEnable>
      <StopButtonReconfigureEnable>false</StopButtonReconfigureEnable>
      <ShippingInformationEnable>true</ShippingInformationEnable>
      <MktActivationEnergy>83.144</MktActivationEnergy>
      <IdealRangeHigh>158</IdealRangeHigh>
      <IdealRangeLow>-22</IdealRangeLow>
      <LcdAlarmCrossCheck>true</LcdAlarmCrossCheck>
      <LcdAlarmTimeDisplayEnable>false</LcdAlarmTimeDisplayEnable>
      <LcdAlarmTimeDisplayOption>Remaining</LcdAlarmTimeDisplayOption>
      <LcdAlarmNumber>Alarm1</LcdAlarmNumber>
      <TemperatureInStartupDelay>false</TemperatureInStartupDelay>
      <ExpirationDateEnable>false</ExpirationDateEnable>
      <TwoAlarmMode>false</TwoAlarmMode>
      <LedBlink>true</LedBlink>
      <LedStopBlink>false</LedStopBlink>
      <RemainingBatteryLife>0</RemainingBatteryLife>
    </UserConfiguration>
    <Passwords>
      <ConfigurationPassword></ConfigurationPassword>
      <DownloadPassword></DownloadPassword>
    </Passwords>
  </Configuration>
  <UserInformation></UserInformation>
  <ProgrammerName></ProgrammerName>
  <StatusWhenRead>
    <ReadTime>1/18/2023 4:57:14 AM</ReadTime>
    <CurrentTime>1/18/2023 3:57:18 AM</CurrentTime>
    <LastTemperature>72.2</LastTemperature>
  </StatusWhenRead>
  <Sensor index="Sensor1">
    <DataPoints>
      <Data number="1" marked="false">71.7</Data>
      <Data number="2" marked="false">71.8</Data>
      <Data number="3" marked="false">71.9</Data>
      <Data number="4" marked="false">71.9</Data>
      <Data number="5" marked="false">71.9</Data>
      <Data number="6" marked="false">71.8</Data>
      <Data number="7" marked="false">71.8</Data>
      <Data number="8" marked="false">71.8</Data>
      <Data number="9" marked="false">71.8</Data>
      <Data number="10" marked="false">71.8</Data>
      <Data number="11" marked="false">71.7</Data>
      <Data number="12" marked="false">71.7</Data>
      <Data number="13" marked="false">71.7</Data>
      <Data number="14" marked="false">71.7</Data>
      <Data number="15" marked="false">71.7</Data>
      <Data number="16" marked="false">71.7</Data>
      <Data number="17" marked="false">71.6</Data>
      <Data number="18" marked="false">71.6</Data>
      <Data number="19" marked="false">71.6</Data>
      <Data number="20" marked="false">71.6</Data>
      <Data number="21" marked="false">71.6</Data>
      <Data number="22" marked="false">71.6</Data>
      <Data number="23" marked="false">71.6</Data>
      <Data number="24" marked="false">71.5</Data>
      <Data number="25" marked="false">71.6</Data>
      <Data number="26" marked="false">71.5</Data>
      <Data number="27" marked="false">71.5</Data>
      <Data number="28" marked="false">71.5</Data>
      <Data number="29" marked="false">71.5</Data>
      <Data number="30" marked="false">71.5</Data>
      <Data number="31" marked="false">71.5</Data>
      <Data number="32" marked="false">71.5</Data>
      <Data number="33" marked="false">71.4</Data>
      <Data number="34" marked="false">71.5</Data>
      <Data number="35" marked="false">71.4</Data>
      <Data number="36" marked="false">71.4</Data>
      <Data number="37" marked="false">71.4</Data>
      <Data number="38" marked="false">71.4</Data>
      <Data number="39" marked="false">71.4</Data>
      <Data number="40" marked="false">71.4</Data>
      <Data number="41" marked="false">71.4</Data>
      <Data number="42" marked="false">71.4</Data>
      <Data number="43" marked="false">71.3</Data>
      <Data number="44" marked="false">71.3</Data>
      <Data number="45" marked="false">71.3</Data>
      <Data number="46" marked="false">71.3</Data>
      <Data number="47" marked="false">71.3</Data>
      <Data number="48" marked="false">71.3</Data>
      <Data number="49" marked="false">71.3</Data>
      <Data number="50" marked="false">71.3</Data>
      <Data number="51" marked="false">71.3</Data>
      <Data number="52" marked="false">71.3</Data>
      <Data number="53" marked="false">71.3</Data>
      <Data number="54" marked="false">71.3</Data>
      <Data number="55" marked="false">71.3</Data>
      <Data number="56" marked="false">71.3</Data>
      <Data number="57" marked="false">71.3</Data>
      <Data number="58" marked="false">71.2</Data>
      <Data number="59" marked="false">71.2</Data>
      <Data number="60" marked="false">71.2</Data>
      <Data number="61" marked="false">71.2</Data>
      <Data number="62" marked="false">71.2</Data>
      <Data number="63" marked="false">71.2</Data>
      <Data number="64" marked="false">71.2</Data>
      <Data number="65" marked="false">71.2</Data>
      <Data number="66" marked="false">71.2</Data>
      <Data number="67" marked="false">71.2</Data>
      <Data number="68" marked="false">71.2</Data>
      <Data number="69" marked="false">71.2</Data>
      <Data number="70" marked="false">71.2</Data>
      <Data number="71" marked="false">71.2</Data>
      <Data number="72" marked="false">71.2</Data>
      <Data number="73" marked="false">71.2</Data>
      <Data number="74" marked="false">71.2</Data>
      <Data number="75" marked="false">71.1</Data>
      <Data number="76" marked="false">71.2</Data>
      <Data number="77" marked="false">71.1</Data>
      <Data number="78" marked="false">71.1</Data>
      <Data number="79" marked="false">71.1</Data>
      <Data number="80" marked="false">71.1</Data>
      <Data number="81" marked="false">71.1</Data>
      <Data number="82" marked="false">71.1</Data>
      <Data number="83" marked="false">71.1</Data>
      <Data number="84" marked="false">71.1</Data>
      <Data number="85" marked="false">71.1</Data>
      <Data number="86" marked="false">71.1</Data>
      <Data number="87" marked="false">71.1</Data>
      <Data number="88" marked="false">71.1</Data>
      <Data number="89" marked="false">71.1</Data>
      <Data number="90" marked="false">71.1</Data>
      <Data number="91" marked="false">71.1</Data>
      <Data number="92" marked="false">71.1</Data>
      <Data number="93" marked="false">71</Data>
      <Data number="94" marked="false">71.1</Data>
      <Data number="95" marked="false">71</Data>
      <Data number="96" marked="false">71</Data>
      <Data number="97" marked="false">71</Data>
      <Data number="98" marked="false">71</Data>
      <Data number="99" marked="false">71</Data>
      <Data number="100" marked="false">71</Data>
    </DataPoints>
    <TestResults index="Sensor1">
      <Passed>true</Passed>
      <TesterName>IMI</TesterName>
      <TestTime>1/18/2023 4:57:15 AM</TestTime>
      <NistDataFileName>CH#31_1_18_2023 01_37_22</NistDataFileName>
      <ChamberStationNumber>31</ChamberStationNumber>
      <ChamberSerialNumber>936644</ChamberSerialNumber>
      <LoggerName>B3C495</LoggerName>
      <TestSetpoint number="1">
        <MonitorSetpoint>70.6</MonitorSetpoint>
        <NistSetpoint>70.8</NistSetpoint>
        <StandardTolerance>5</StandardTolerance>
        <ResultTolerance>0.2</ResultTolerance>
        <SetpointTime>04:17:42</SetpointTime>
      </TestSetpoint>
      <TestSetpoint number="2">
        <MonitorSetpoint>70.6</MonitorSetpoint>
        <NistSetpoint>70.8</NistSetpoint>
        <StandardTolerance>5</StandardTolerance>
        <ResultTolerance>0.2</ResultTolerance>
        <SetpointTime>04:17:42</SetpointTime>
      </TestSetpoint>
      <TestSetpoint number="3">
        <MonitorSetpoint>70.6</MonitorSetpoint>
        <NistSetpoint>70.8</NistSetpoint>
        <StandardTolerance>5</StandardTolerance>
        <ResultTolerance>0.2</ResultTolerance>
        <SetpointTime>04:17:42</SetpointTime>
      </TestSetpoint>
    </TestResults>
    <SensorType>BIO</SensorType>
    <SensorUnit>F</SensorUnit>
    <Alarms>
      <Alarm number="1">
        <AlarmEnabled>true</AlarmEnabled>
        <LogicEqual>false</LogicEqual>
        <AlarmType>HighTempLimitSingle</AlarmType>
        <AlarmTriggered>true</AlarmTriggered>
        <AlarmTriggerTime>1/18/2023 12:37:53 AM</AlarmTriggerTime>
        <AlarmSetpoints>
          <HiSetpoint1>41</HiSetpoint1>
          <LoSetpoint1>-22</LoSetpoint1>
          <HiSetpoint2>158</HiSetpoint2>
          <LoSetpoint2>-22</LoSetpoint2>
        </AlarmSetpoints>
        <AlarmCounter>402</AlarmCounter>
        <AlarmTimeThreshold>4</AlarmTimeThreshold>
      </Alarm>
      <Alarm number="2">
        <AlarmEnabled>true</AlarmEnabled>
        <LogicEqual>false</LogicEqual>
        <AlarmType>LowTempLimitSingle</AlarmType>
        <AlarmTriggered>false</AlarmTriggered>
        <AlarmTriggerTime>1/1/1970 12:00:00 AM</AlarmTriggerTime>
        <AlarmSetpoints>
          <HiSetpoint1>158</HiSetpoint1>
          <LoSetpoint1>32</LoSetpoint1>
          <HiSetpoint2>158</HiSetpoint2>
          <LoSetpoint2>-22</LoSetpoint2>
        </AlarmSetpoints>
        <AlarmCounter>0</AlarmCounter>
        <AlarmTimeThreshold>4</AlarmTimeThreshold>
      </Alarm>
      <Alarm number="3">
        <AlarmEnabled>true</AlarmEnabled>
        <LogicEqual>false</LogicEqual>
        <AlarmType>LowTempLimitSingle</AlarmType>
        <AlarmTriggered>false</AlarmTriggered>
        <AlarmTriggerTime>1/1/1970 12:00:00 AM</AlarmTriggerTime>
        <AlarmSetpoints>
          <HiSetpoint1>158</HiSetpoint1>
          <LoSetpoint1>-22</LoSetpoint1>
          <HiSetpoint2>158</HiSetpoint2>
          <LoSetpoint2>-22</LoSetpoint2>
        </AlarmSetpoints>
        <AlarmCounter>0</AlarmCounter>
        <AlarmTimeThreshold>4</AlarmTimeThreshold>
      </Alarm>
      <Alarm number="4">
        <AlarmEnabled>true</AlarmEnabled>
        <LogicEqual>false</LogicEqual>
        <AlarmType>HighTempLimitSingle</AlarmType>
        <AlarmTriggered>false</AlarmTriggered>
        <AlarmTriggerTime>1/1/1970 12:00:00 AM</AlarmTriggerTime>
        <AlarmSetpoints>
          <HiSetpoint1>158</HiSetpoint1>
          <LoSetpoint1>-22</LoSetpoint1>
          <HiSetpoint2>158</HiSetpoint2>
          <LoSetpoint2>-22</LoSetpoint2>
        </AlarmSetpoints>
        <AlarmCounter>0</AlarmCounter>
        <AlarmTimeThreshold>4</AlarmTimeThreshold>
      </Alarm>
      <Alarm number="5">
        <AlarmEnabled>false</AlarmEnabled>
        <LogicEqual>false</LogicEqual>
        <AlarmType>HighTempLimitSingle</AlarmType>
        <AlarmTriggered>false</AlarmTriggered>
        <AlarmTriggerTime>1/1/1970 12:00:00 AM</AlarmTriggerTime>
        <AlarmSetpoints>
          <HiSetpoint1>158</HiSetpoint1>
          <LoSetpoint1>-22</LoSetpoint1>
          <HiSetpoint2>158</HiSetpoint2>
          <LoSetpoint2>-22</LoSetpoint2>
        </AlarmSetpoints>
        <AlarmCounter>0</AlarmCounter>
        <AlarmTimeThreshold>4</AlarmTimeThreshold>
      </Alarm>
      <Alarm number="6">
        <AlarmEnabled>false</AlarmEnabled>
        <LogicEqual>false</LogicEqual>
        <AlarmType>HighTempLimitSingle</AlarmType>
        <AlarmTriggered>false</AlarmTriggered>
        <AlarmTriggerTime>1/1/1970 12:00:00 AM</AlarmTriggerTime>
        <AlarmSetpoints>
          <HiSetpoint1>158</HiSetpoint1>
          <LoSetpoint1>-22</LoSetpoint1>
          <HiSetpoint2>158</HiSetpoint2>
          <LoSetpoint2>-22</LoSetpoint2>
        </AlarmSetpoints>
        <AlarmCounter>0</AlarmCounter>
        <AlarmTimeThreshold>4</AlarmTimeThreshold>
      </Alarm>
    </Alarms>
  </Sensor>
  <ShipmentInformation>
    <ShipmentTag number="1">
      <TagDefID></TagDefID>
      <LOVPossibleValue></LOVPossibleValue>
      <Label></Label>
      <Value></Value>
      <FlexTagType>Shipment</FlexTagType>
    </ShipmentTag>
    <ShipmentTag number="2">
      <TagDefID></TagDefID>
      <LOVPossibleValue></LOVPossibleValue>
      <Label></Label>
      <Value></Value>
      <FlexTagType>Shipment</FlexTagType>
    </ShipmentTag>
    <ShipmentTag number="3">
      <TagDefID></TagDefID>
      <LOVPossibleValue></LOVPossibleValue>
      <Label></Label>
      <Value></Value>
      <FlexTagType>Shipment</FlexTagType>
    </ShipmentTag>
    <ShipmentTag number="4">
      <TagDefID></TagDefID>
      <LOVPossibleValue></LOVPossibleValue>
      <Label></Label>
      <Value></Value>
      <FlexTagType>Shipment</FlexTagType>
    </ShipmentTag>
    <ShipmentTag number="5">
      <TagDefID></TagDefID>
      <LOVPossibleValue></LOVPossibleValue>
      <Label></Label>
      <Value></Value>
      <FlexTagType>Shipment</FlexTagType>
    </ShipmentTag>
    <ShipmentTag number="6">
      <TagDefID></TagDefID>
      <LOVPossibleValue></LOVPossibleValue>
      <Label></Label>
      <Value></Value>
      <FlexTagType>Shipment</FlexTagType>
    </ShipmentTag>
    <ShipmentTag number="7">
      <TagDefID></TagDefID>
      <LOVPossibleValue></LOVPossibleValue>
      <Label></Label>
      <Value></Value>
      <FlexTagType>Shipment</FlexTagType>
    </ShipmentTag>
    <ShipmentTag number="8">
      <TagDefID></TagDefID>
      <LOVPossibleValue></LOVPossibleValue>
      <Label></Label>
      <Value></Value>
      <FlexTagType>Shipment</FlexTagType>
    </ShipmentTag>
    <ShipmentTag number="9">
      <TagDefID></TagDefID>
      <LOVPossibleValue></LOVPossibleValue>
      <Label></Label>
      <Value></Value>
      <FlexTagType>Shipment</FlexTagType>
    </ShipmentTag>
    <ShipmentTag number="10">
      <TagDefID></TagDefID>
      <LOVPossibleValue></LOVPossibleValue>
      <Label></Label>
      <Value></Value>
      <FlexTagType>Shipment</FlexTagType>
    </ShipmentTag>
  </ShipmentInformation>
  <LocalInfo>
    <TimeZone>Central Europe Standard Time</TimeZone>
    <UtcOffsetMinutes>60</UtcOffsetMinutes>
    <UserName>imi</UserName>
    <Culture>en-US</Culture>
  </LocalInfo>
  <AssemblyInfo>
    <AssemblyName>Sensitech.Devices</AssemblyName>
    <AssemblyVersion>2.3.0.0</AssemblyVersion>
    <ClassName>TTUltra</ClassName>
    <FileVersion>1.0</FileVersion>
  </AssemblyInfo>
  <AdditionalAssemblyInfo>
    <Station>SENSI-27</Station>
    <AssemblyName>TurboValidate Plus</AssemblyName>
    <AssemblyVersion>2.5.2207.802</AssemblyVersion>
  </AdditionalAssemblyInfo>
  <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
    <SignedInfo>
      <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
      <SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
      <Reference URI="">
        <Transforms>
          <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
        </Transforms>
        <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
        <DigestValue>L6z3IVi0JHgIsyy68K1aEkyTqTPf4hf1r3nmvNZsLEc=</DigestValue>
      </Reference>
    </SignedInfo>
    <SignatureValue>Iwhh0FJTfiACl2nbpOjmHUnawOxQ8Zd B89EX1FD0xFNxZrT9eN7WQ3J7BfxhJXrek7ejlhWNI3FU6AGp0y5EU3P2e crYSgd3xNzkB3KMeH0W5owElGMYNAjfqWrWZybo3Bz67JQ116dDex8EpoMU/3rtEM/7Hl8i1NTI9hBvU=</SignatureValue>
  </Signature>
</Data>

My question is: I need to take the value which is located between the <TestResults index="Sensor1"> tags, which is the ( <Passed>true</Passed>) I tried many things but I didn't manage to do it and I need only the Test result if is Passed or Failed. Thanks!

so how can I take this value and put it into a variable?

Thanks a lot!!

CodePudding user response:

@ECHO OFF
SETLOCAL
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\xmldata.xml"
SET "index="

FOR /f "usebackqtokens=1,2delims=<> " %%g IN ("%filename1%") DO (
 IF DEFINED index IF "%%g"=="Passed" SET "passed=%%h"
 IF DEFINED index GOTO done
 IF "%%g"=="TestResults" SET %%h
)

:done
IF DEFINED passed (ECHO FOR %index% - %passed%) ELSE (ECHO NOT found)

GOTO :EOF

Always verify against a test directory before applying to real data.

Note that if the filename does not contain separators like spaces, then both usebackq and the quotes around %filename1% can be omitted.

Read the file, tokenising each line using <>Space as delimiters. Select the first 2 tokens to %%g and %%h.

If the first token is "Testresults", execute set %%h. %%h will be index="something", so the variable index will be set to "something".

Read the next line. If index has been set and the first token is Passed, then set passed to the second token and exit.

If index has been set but the first token on the next line is not Passed, then exit (presumed error condition)

Report results.

CodePudding user response:

If there is just one <Passed>true|false</Passed> line in all files, then the solution is very simple:

@echo off
for %%f in (*.xml) do for /F "tokens=3 delims=<>" %%a in ('findstr "Passed" "%%f"') do echo %%f: %%a

If there are several Passed lines and you want the one that is after <TestResults index="Sensor1"> one, then use this method:

@echo off
setlocal

rem Process all *.xml files
for %%f in (*.xml) do (

   rem Get the line number of "TestResults index=Sensor1"
   for /F "delims=:" %%i in ('findstr /N /C:"TestResults index=\"Sensor1\"" "%%f"') do (

      rem Show the result from first "Passed" line after "TestResults"
      set "first="
      for /F "delims=:<> tokens=1,4" %%a in ('findstr /N "Passed" "%%f"') do (
         if not defined first  if %%a gtr %%i (
            echo %%f: %%b
            set "first=True"
         )
      )

   )
)
  • Related