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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:10:08+00:00 2026-06-13T12:10:08+00:00

I try to parse a ini file into properties which I can use in

  • 0

I try to parse a ini file into properties which I can use in my ant script. I have the following:

<project name="DeployScript" default="deploy-staging" basedir=".">
    <taskdef resource="net/sf/antcontrib/antcontrib.properties" />

    <!-- The location of the settings.ini file -->
    <property name="ini-file" location="../settings.ini" />

    <loadfile property="iniConfig" srcFile="${ini-file}"/>  

    <target name="deploy-staging" 
        description="Deploy project to staging environment" >
        <echo message="Ini file: ${ini-file}" />
        <echo message="${lib}" />
        <echo message="${store_dir}" />
        <echo message="${ant.home}" />

        <!--- walk the ini file's lines -->
        <foreach list="${iniConfig}"
            target="echoMsg"
            param="line" 
            delimiter="${line.separator}" />

        <echo message="HERE: ${prevSection}" />
    </target>

    <property name="prevSection" value="" />

    <!-- this is executed for every line in the ini file. -->
    <target name="echoMsg">  
        <!-- strip out the section name, variable name and value (if exists on the line) -->
        <propertyregex property="secm"
            input="${line}"
            regexp="^\[(.*)\]"
            select="\1"
            casesensitive="false" />
        <propertyregex property="name"
            input="${line}"
            regexp="^([\S]+)\s*=\s*([^;]+)"
            select="\1"
            casesensitive="false"
            defaultValue="" /> 
        <propertyregex property="value"
            input="${line}"
            regexp="^([\S]+)\s*=\s*([^;]+)"
            select="\2"
            casesensitive="false"
            defaultValue="" />

        <!-- overwrite the previous section if we have found a new one. -->
        <if>
            <isset property="secm" />
            <then>
                <echo message="PREVSECTION IS SET" />   
                <property name="prevSection" value="${secm}" />
            </then>
        </if>

        <!-- display the information about the found data -->       
        <echo message="line    = ${line}" />
        <echo message="section = ${secm}" />
        <echo message="name    = ${name}" />        
        <echo message="value   = ${value}" />
        <echo message="new last section: ${prevSection}" />
        <echo message="----" />             
    </target>   
</project>

What I try to do is parse all name=value pairs and put them in properties like: section.name=value;

Somehow the section is not remembered within the “echoMsg” target. I would like the section name to be remembered.

So,

[global]
name=var
name2=val

[section2]
name=var

Should become:

global.name=var
global.name2=val
section2.name=var

This is the output of my ant script:

echoMsg:
 [echo] PREVSECTION IS SET
 [echo] line    = [global]
 [echo] section = global
 [echo] name    =
 [echo] value   =
 [echo] new last section: global
 [echo] ----

echoMsg:
 [echo] line    = servername = my-server.local ; Server name
 [echo] section = ${secm}
 [echo] name    = servername
 [echo] value   = mac-mini-van-Peter.local7
 [echo] new last section: ${prevSection}
 [echo] ----

As you can see, the last “${prevSection}” is not set. I would expect it to be “global”.

I tried to use instead of property, but there’s no difference.

  • 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-13T12:10:09+00:00Added an answer on June 13, 2026 at 12:10 pm

    Here’s a hint: Try adding an echo section before the <propertyregex> statements to see what the values of the various properties.

    I added these <echo> lines…

    <target name="echoMsg">
        <!-- strip out the section name, variable name and value (if exists on the line) -->
        <echo message="prev line    = ${line}" />
        <echo message="prev section = ${secm}" />
        <echo message="prev name    = ${name}" />
        <echo message="prev value   = ${value}" />
        <echo message="prev new last section: ${prevSection}" />
        <echo message="----" />
    

    Now let’s look at the output:

    deploy-staging:
         [echo] Ini file: /Users/david/property.ini
         [echo] ${lib}
         [echo] ${store_dir}
         [echo] /usr/share/ant
    
    echoMsg:
         [echo] prev line    = [global]
         [echo] prev section = ${secm}
         [echo] prev name    = ${name}
         [echo] prev value   = ${value}
         [echo] prev new last section: 
         [echo] ----
         [echo] PREVSECTION IS SET
         [echo] line    = [global]
         [echo] section = global
         [echo] name    = 
         [echo] value   = 
         [echo] new last section: 
         [echo] ----
    
    echoMsg:
         [echo] prev line    = name=foo
         [echo] prev section = ${secm}
         [echo] prev name    = ${name}
         [echo] prev value   = ${value}
         [echo] prev new last section: 
         [echo] ----
         [echo] line    = name=foo
         [echo] section = ${secm}
         [echo] name    = name
         [echo] value   = foo
         [echo] new last section: 
         [echo] ----
    
    echoMsg:
         [echo] prev line    = name2=bar
         [echo] prev section = ${secm}
         [echo] prev name    = ${name}
         [echo] prev value   = ${value}
         [echo] prev new last section: 
         [echo] ----
         [echo] line    = name2=bar
         [echo] section = ${secm}
         [echo] name    = name2
         [echo] value   = bar
         [echo] new last section: 
         [echo] ----
    
    echoMsg:
         [echo] prev line    = [section2]
         [echo] prev section = ${secm}
         [echo] prev name    = ${name}
         [echo] prev value   = ${value}
         [echo] prev new last section: 
         [echo] ----
         [echo] PREVSECTION IS SET
         [echo] line    = [section2]
         [echo] section = section2
         [echo] name    = 
         [echo] value   = 
         [echo] new last section: 
         [echo] ----
    
    echoMsg:
         [echo] prev line    = name=fubar
         [echo] prev section = ${secm}
         [echo] prev name    = ${name}
         [echo] prev value   = ${value}
         [echo] prev new last section: 
         [echo] ----
         [echo] line    = name=fubar
         [echo] section = ${secm}
         [echo] name    = name
         [echo] value   = fubar
         [echo] new last section: 
         [echo] ----
         [echo] HERE: 
    
    BUILD SUCCESSFUL
    Total time: 1 second
    

    Each call to <echomsg> loses the values previously set in <echomsg>.

    I suggest you try the <for> task. The <for> task won’t lose the values of your property through each iteration of the loop. However, you’ll have to add the override setting to your <propertyregex> tasks, and to use the <var> tasks instead of <property> tasks to be able to reset your property values through each iteration of the loop.

    You’ll also have to change your <taskdef> to this:

    <taskdef resource="net/sf/antcontrib/antlib.xml" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a xml file which contains arabic characters.When i try to parse a
I have following script which processes emails and save them to csv file. there
I have a JSON which I get from web application. I try this parse
I try to parse articles from wikipedia. I use the *page-articles.xml file, where they
I try to parse html file and to generate pdf. I use code document.Open();
I have the following code in my application which does two things: Parse the
I try to parse a file in my DatabaseHandler class but Eclipse say: The
I try to parse encrypted &RQ history, but i really can't understand asm code.
I have a problem with sax parser and encoded text. I try to parse
i try to parse an rdf file using android.sax and android.utils.Xml methods. My parser

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.