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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:19:04+00:00 2026-05-11T16:19:04+00:00

I have a directory of files for which I’d like to do in-place string

  • 0

I have a directory of files for which I’d like to do “in-place” string filtering using Apache Ant (version 1.7.1 on Linux).

For example, suppose that in directory mydir I have files foo, bar, and baz. Further suppose that all occurences of the regular expression OLD([0-9]) should be changed to NEW\1, e.g. OLD2 → NEW2. (Note that the replace Ant task won’t work because it does not support regular expression filtering.)

This test situation can be created with the following Bash commands (ant will be run in the current directory, i.e. mydir‘s parent directory):

mkdir mydir
for FILE in foo bar baz ; do echo "A OLD1 B OLD2 C OLD3" > mydir/${FILE} ; done

Here is my first attempt to do the filtering with Ant:

<?xml version="1.0"?>
<project name="filter" default="filter">
    <target name="filter">
        <move todir="mydir">
            <fileset dir="mydir"/>
            <filterchain>
                <tokenfilter>
                    <replaceregex pattern="OLD([0-9])" replace="NEW\1" flags="g"/>
                </tokenfilter>
            </filterchain>
        </move>
    </target>
</project>

Running this first Ant script has no effect on the files in mydir. The overwrite parameter is true by default with the move Ant task. I even fiddled with the granularity setting, but that didn’t help.

Here’s my second attempt, which “works,” but is slightly annoying because of temporary file creation. This version filters the content properly by moving the content to files with a filtered suffix, then the filtered content is “moved back” with original filenames:

<?xml version="1.0"?>
<project name="filter" default="filter">
    <target name="filter">
        <move todir="mydir">
            <globmapper from="*" to="*.filtered"/>
            <fileset dir="mydir"/>
            <filterchain>
                <tokenfilter>
                    <replaceregex pattern="OLD([0-9])" replace="NEW\1" flags="g"/>
                </tokenfilter>
            </filterchain>
        </move>
        <move todir="mydir">
            <globmapper from="*.filtered" to="*"/>
            <fileset dir="mydir"/>
        </move>
    </target>
</project>

Can the first attempt (without temporary files) be made to work?

  • 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-05-11T16:19:04+00:00Added an answer on May 11, 2026 at 4:19 pm

    See the replace task:

    <replace 
       dir="mydir" 
       includes="foo, bar, baz">
       <replacefilter token="OLD" value="NEW" />
    </replace>
    

    or the replaceregexp task:

    <replaceregexp
        file="${src}/build.properties"
        match="OldProperty=(.*)"
        replace="NewProperty=\1"
        byline="true"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If, for example, I have a directory which contains the following files: Test-20120626-1023.txt Test-20120626-0710.txt
I have PDB(text) files which are in a directory. I would like to print
I have a directory containing several files, some of which have spaces in their
I have a directory with directorybrowsing enabled which lists the files successfully but what
I have a project in which i have the following files main.wxs - directory
I have the following code to recursively search for files through a directory, which
I have a directory in which I keep adding different C++ source files, and
I have like 50 different php files which all use the mail function. After
I have a directory which contains files and a number of levels of subdirectories:
I have written a function which scans all the files/directory in a given directory

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.