user3762977
user3762977

Reputation: 691

Apache ant character selector in filename copy task

For a fileset like this:

XXX_staging
    A_filename\A_filename.txt
    B_filenAme\B_filenAme.txt

XXX_AL_staging

The following ant target will copy only the files whose names start with an uppercase "A", from the XXX_staging folder to the XXX_AL_staging folder:

<target name="split_topics">
      <copy todir="XXX_AL_staging\" verbose="true" overwrite="true">
        <fileset dir=".\XXX_staging" includes="**/A*.txt">
        </fileset>
  </copy>
 </target>

What I want to do is copy only files whose names start with A through L. This doesn't work:

<target name="split_topics">
      <copy todir="XXX_AL_staging\" verbose="true" overwrite="true">
        <fileset dir=".\XXX_staging" includes="**/[A-L]*.txt">
        </fileset>
  </copy>
 </target>

I'm wondering if the problem is that the second is not just a wildcard match like the first, but a regular expression match, which won't work for a filename? I don't want to match the contents of the files, just the filenames, so containsregexp seems to be out.

How do I do this?

Upvotes: 2

Views: 3130

Answers (2)

Mark O&#39;Connor
Mark O&#39;Connor

Reputation: 77971

Filename selectors can utilize a regular expression.

Example 1

├── build.xml
├── src
│   └── somedir
│       ├── A_filename.txt
│       ├── B_filename.txt
│       ├── C_filename.txt
│       ├── D_filename.txt
│       ├── E_filename.txt
│       ├── F_filename.txt
│       └── G_filename.txt
└── target
    └── somedir
        ├── A_filename.txt
        ├── B_filename.txt
        ├── C_filename.txt
        └── D_filename.txt

build.xml

<project name="demo" default="copy">

  <property name="src.dir"   location="src"/>
  <property name="build.dir" location="target"/>

  <target name="copy">
    <copy todir="${build.dir}" overwrite="true" verbose="true">
      <fileset dir="${src.dir}">
        <filename regex="\/[A-D].*.txt$"/>
      </fileset>
    </copy>
  </target>

</project>

Example 2

Upvotes: 4

bmargulies
bmargulies

Reputation: 100050

Unfortunately, ant patterns are not regular expressions, not even a little. They are wildcards with a very limited syntax. For a complex solution, you can use the ability to define your own task in Javascript and code something out, but this will not let you create an alternative file set pattern matching syntax.

Upvotes: 0

Related Questions