A batch file processed by the Windows Command Processor cmd.exe is the worst choice for processing an XML file because there is no built-in support for real XML processing.
The following batch file code could be used if the XML file is always structured as shown in the question.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "Value=1"
if not exist "File.xml" goto ProcessValue
set "NameTagFound="
for /F "tokens=2,3 delims=<>" %%I in ('%SystemRoot%\System32\findstr.exe /R "<name>H_YPE</name> <value>[0123456789]*</value>" "File.xml"') do (
if "%%I" == "name" (
if /I "%%J" == "H_YPE" set "NameTagFound=1"
) else if defined NameTagFound (
if "%%I" == "value" (
if not "%%J" == "" set "Value=%%J"
goto ProcessValue
)
)
)
:ProcessValue
echo Value is: %Value%
endlocal
Please note following requirements for the structure of the data in the XML file:
- There must be leading spaces/tabs left to the start tags
<name> and <value> because of tokens=2,3 delims=<> and the two elements must be on separate lines not containing any other element or attributes.
- An element
name with value H_YPE must exist only within element variable and not within another element because of the code does not check where <name>H_YPE</name> is found in the file.
- The element
name with value H_YPE must be inside the element variable on a separate line above the line with the element value. So the order of the elements name and value cannot be changed without adapting the code.
- The element
variable with the element name with value H_YPE must have also the element value or the value of next element value found anywhere below the line with <name>H_YPE</name> is assigned wrongly to the environment variable Value instead of using the default value 1.
- The value
H_YPE must be always in the XML file with this spelling (all letters in upper case).
The code could be enhanced to do more XML structure checks if one of the five requirements is not always fulfilled by the XML file contents.
XML tags are case-sensitive according to XML specification. Therefore this code runs FINDSTR also case-sensitive although the value H_YPE could be in any case per XML specification. The IF conditions comparing strings are also case-sensitive for the same reason including the IF condition which checks for value H_YPE.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
echo /?
endlocal /?
findstr /?
for /?
goto /?
if /?
set /?
setlocal /?