I am a beginner in writing batch scripts and your help on this is very much apreciated.
Below is the xml and I need to extract all names whose flag is “on” to a txt file. There are several other category tag instances.
<Head>
<Category
name="RIVERTD"
flag="on"
location="SG002">
</Category>
<Category
name="BRETRED"
flag="on"
location="IT213">
</Category>
<Category
name="AMERAND"
flag="off"
location="US212">
</Category>
</Head>
So, the output I’m looking is as below
RIVERTD
BRETRED
I tried using below code
@echo off
setlocal disableDelayedExpansion
set input="CP.xml"
set output="Names.txt"
if exist %output% del %output%
for /f "delims=" %%A in ('findstr /n /c:"name=" %input%') do (
set "ln=%%A"
setlocal enableDelayedExpansion
call :parseLine
endlocal
)
type %output%
exit /b
:parseLine
set "ln2=!ln:*name=!"
if "!ln2!"=="!ln!" exit /b
for /f tokens^=2^ delims^=^" %%B in ("!ln2!") do (
setlocal disableDelayedExpansion
>>%output% echo(%%B
endlocal
)
set "ln=!ln2!"
goto :parseLine
This gives me the result
RIVERTD
BRETRED
AMERAND
However this code doesn’t filter the names based on flag. I’m a beginner. Kindly help to add filter based on flag. Many thanks.
This problem may be solved with a very interesting trick!:
EDIT: Some explanations adeded
The file have several lines, but the OP look for lines that have the form of an assignment, like these:
My program does NOT check for any name, but execute any line with a value after the equal sign as an assignment. This way, when my program process previous lines, the result is equivalent to execute the following commands:
After that, just check if FLAG variable have the value “on”; if so, then the NAME variable have the target value, because it was assigned in previous line.
Antonio