colbype
colbype

Reputation: 11

Ant script split file name and extension into 2 separate properties

I have a fileset that has say 10 files with extensions. I need to then take this and get the filename and extension into 2 separate properties/variables but not sure how. The reason I need to do this as a file starts out on Unix as XB12345.FILE which I need to move to an I/5 system with the file name as XB12345.FILE and the member as FILE.MBR. Most of the files do not have a consistent extension and will only be known at run time. Any help would be appreciated.

Upvotes: 0

Views: 2863

Answers (2)

David W.
David W.

Reputation: 107040

This sounds like a job for the Ant-Contrib tasks.

The Ant-Contrib tasks have been around for a long time and are quite common to see in Ant projects. The PropertyRegex task will do what you want. In fact, there's even a For task that can handle loops.

The <PropertyRegEx> task would look something like this:

<propertyregex property="base.name"
    input="file.name"
    regexp="(.*)\.(.*)"
    select="\1"/>

<propertyregex property="suffix.name"
    input="file.name"
    regexp="(.*)\.(.*)"
    select="\2"/>

Installing the Ant-Contrib tasks is fairly easy if you follow these steps:

  • Download the ant-contrib-1.0b3.jar.
  • Create a directory in the root of your project called antlib/ac.
  • Put the ant-contrib-1.0b3.jar file in theantlib/ac` directory.
  • Add the task definition.

Like this:

<taskdef resource="net/sf/antcontrib/antlib.xml">
    <classpath>
        <fileset dir="${basedir}/antlib/ac"/>
    <classpath>
</taskdef>

If you use a version control system, check in the ant-contrib-1.0b3.jar file into your version control system for the project. Now, when someone needs to do a build, they checkout your project, and the Ant Contrib tasks are already defined and the Ant-Contrib jar is there. There's no need for the developer to install Ant-Contrib. Your build just works.

Upvotes: 5

Vishal
Vishal

Reputation: 3279

Though propertyregex is the solution for the intended problem, but you must use override property for you solution.

<propertyregex property="base.name"
    input="file.name"
    regexp="(.*)\.(.*)"
    select="\1" override="true"/>

<propertyregex property="suffix.name"
    input="file.name"
    regexp="(.*)\.(.*)"
    select="\2" override="true"/>

Upvotes: 0

Related Questions