Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8995527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:37:55+00:00 2026-06-15T23:37:55+00:00

I am a beginner in writing batch scripts and your help on this is

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:37:56+00:00Added an answer on June 15, 2026 at 11:37 pm

    This problem may be solved with a very interesting trick!:

    @echo off
    setlocal EnableDelayedExpansion
    (for /F "tokens=1,2 delims== " %%a in (CP.xml) do (
       if "%%~b" neq "" set %%a=%%~b
       if /I "!flag!" equ "on" echo !name!& set flag=
    )) > Names.txt
    

    EDIT: Some explanations adeded

    The file have several lines, but the OP look for lines that have the form of an assignment, like these:

      name="RIVERTD"
      flag="on"
    
      name="BRETRED"
      flag="on"
    
      name="AMERAND"
      flag="off"
    

    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:

      set name="RIVERTD"
      set flag="on"
    
      set name="BRETRED"
      set flag="on"
    
      set name="AMERAND"
      set flag="off"
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im still a beginner in C programming and I need a little help writing
being a regex beginner, I need some help writing a regex. It should match
JS beginner here. I need help with a script to place different content in
I am a beginner at writing android apps. Can you please help me how
I am writing a test code (I'm just a beginner) where I need to
I am a C beginner, and I am writing a very simple linked-list. I
A very common beginner mistake when writing recursive functions is to accidentally fire off
Some beginner help needed if anybody can.. I'm writing a contact form with a
I am beginner when it comes to writing makefiles but I am having this
I am a beginner and writing code to display data in a Gridview using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.