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 7776217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:49:30+00:00 2026-06-01T17:49:30+00:00

I have this script, to read xml file. The file contains coordinates and I

  • 0

I have this script, to read xml file. The file contains coordinates and I want to list the coordinates:

@echo off
setlocal EnableDelayedExpansion

FOR %%K IN (*.xml) DO (
SET K=%%K
SET K=!K:~0,-4!
SET "prep=0"

    REM READ DATA
    FOR /F "tokens=*" %%X IN (!K!.kml) DO (
    if !prep! == 1 (
    echo %%X
    pause
      FOR /F %%L IN ("%%X") DO (
      SET L=%%L
      IF NOT "!L:~0,1!" == "<" (
        echo %%L
        )       
      )
      SET "prep=0"
    )
    if "%%X" == "<coordinates>" ( SET "prep=1" )
    )
)

I got these result:

14.63778004128814,49.50141683426452,0 14.63696238385996,49.48348965654706,0 14.6
8840586504191,49.47901033971912,0 14.68589371304878,49.49939179836829,0 14.63778
004128814,49.50141683426452,0 </coordinates>
Press and key to continue...
14.63778004128814,49.50141683426452,0
Press and key to continue...

First you see the line with coordinates. Second, in the 3rd loop, there are coordinates printed. But I have only one pair of coordinates printed… If I will press a key again, the batch finishes without printing next columns. Can you help?

Edit
After the answer has been posted, I have question 1) could we use this:

SET LF=^


setlocal EnableDelayedExpansion
... (next code) ...
set "var=!var: =%LF%!"    

So when there is no delayed LF variable, we could embed it. Or not?

And 2) why in your code

for %%L in ("!LF!") do set "X=!X: =%%~L!"

Did you use %%~L and not just %%L

  • 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-01T17:49:32+00:00Added an answer on June 1, 2026 at 5:49 pm

    Your immediate problem is that FOR /F does not iterate the tokens in a line. It simply parses each token that you ask for. If you don’t specify a “tokens” option, then it defaults to “tokens=1” – it only parses the first token in the line.

    However, FOR /F will treat a string as multiple lines if the string contains linefeed characters. It will then iterate each line like you want. The trick is to replace your space delimiter with a line feed character. There are multiple methods that can do the job, but I will show what I think is the easiest to work with.

    First define a variable containing a single linefeed

    set LF=^
    
    
    ::The two blank lines above are critical for the definition of the line feed
    

    The next trick is to replace spaces in your variable with linefeeds. Normally substituion using a variable for the replacement would look something like set "var=!var:search=%replaceVar%!". But that won’t work for the LF variable – it is difficult to work with the LF variable using normal expansion. It is much easier to use delayed expansion. We can’t embed delayed expansion within delayed expansion, but we can transfer the value of LF to a simple FOR variable and use for %%L in ("!LF!") do set "var=!var: =%%~L!"

    One thing about your code I do not understand – your initial FOR loop is iterating accross all the .KML files. You strip off the extension using a substring operation. There is a much easier way to do that without using an environment variable: %%~nK will give the base name of the file without the extension. But why do that at all when you turn around and append the extension again?

    I used the %%K value directly – I added the USEBACKQ option and added quotes to allow for spaces in the file name.

    Here is code that should do what you are expecting.

    @echo off
    setlocal EnableDelayedExpansion
    
    ::define a variable containing a linefeed character
    set LF=^
    
    
    ::Above 2 blank lines are part of the LF definition, do not remove
    
    for %%K in (*.kml) do (
      set "prep=0"
      for /f "usebackq tokens=*" %%X in ("%%K") do (
        if !prep! == 1 (
          echo %%X
          pause
          set "ln=%%X"
          for %%L in ("!LF!") do set "ln=!ln: =%%~L!"
          for /f %%L in ("!ln!") do (
            set L=%%L
            if not "!L:~0,1!" == "<" (
              echo %%L
            )
          )
          set "prep=0"
        )
        if "%%X" == "<coordinates>" ( set "prep=1" )
      )
    )
    

    BUT – I think you have a bigger problem. I am worried that you are setting yourself up for a world of pain by using batch to parse XML. You are assuming the XML will always be layed out the same way. There are countless valid ways of adding or subtracting linefeeds and white space into the XML document that would break your algorithm. Can you be sure all your input files came from the same source and will always be formatted like you expect? I think you really should be using XSLT to parse and transform your XML document into a naked list of coordinates.

    Answsers to additional questions

    1) set "var=!var: =%LF%!" will not work – Regular expansion of LF requires escape sequences and multiple expansions. This will work: set "var=!var: =^%LF%LF%!"

    The escape sequences for %LF% can get very tricky, so I try to avoid them.

    2) Regarding for %%L in ("!LF!") do set "X=!X: =%%~L!", note that it is a simple FOR, not FOR /F. The !LF! must be quoted or else FOR will not read it. But the FOR statement preserves the quotes (unlike FOR /F), so I need %%~L to remove the enclosing quotes.

    There is a very important distinction between FOR and FOR /F with regard to linefeeds. FOR will preserve quoted linefeeds, whereas FOR /F treats the linefeed as a line delimiter and iterates each line, so the linefeeds are not preserved.

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

Sidebar

Related Questions

I have this script to generate an XML file for an RSS feed. Works
how to read the multiple values from XML file using perl script? i have
I have this script which basically toggles a bgColor class on and off so
I have this script that collects data from users and I want to check
I'm trying to read xml file from vbs script. Xml is encoded in utf-8
I have a python script that parsing an xml file and is returning the
I have an xml file which I imported in my html page like this:
I have a simple xml file that looks like this: <?xml version=1.0 encoding=UTF-8 standalone=yes
I have this script: select name,create_date,modify_date from sys.procedures order by modify_date desc I can
i have this script $content = string if(!isset($_GET['page'])){ $page = 1; } else{ $page

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.