trilawney
trilawney

Reputation: 1792

Multiple renaming using ant script

Let me explain the scenario:

D:\project\src\one.txt
D:\project\src\two.txt
D:\project\src\three.txt
D:\project\src\four.txt

The above files should be copied as :

D:\project\dst\one.xls
D:\project\dst\two.xls
D:\project\dst\three.xls
D:\project\dst\four.xls

I need to change the extension without using the mapper and move task. I need to rename as above using a for loop with fte:filecopy function inside. Is this possible ???

Upvotes: 0

Views: 3292

Answers (3)

ewan.chalmers
ewan.chalmers

Reputation: 16235

For anyone arriving here without the negative requirement afflicting the OP, the much simpler answer is to use a mapper.

<project default="move_files">
  <target name="move_files">
    <copy todir="dst">
      <fileset dir="src">
        <include name="*.txt"/>
      </fileset>
      <globmapper from="*.txt" to="*.xls"/>
    </copy>
  </target>
</project>

Upvotes: 6

DoctorRuss
DoctorRuss

Reputation: 1439

Can you slice it the other way and perform the renaming inside the fte:filecopy command? Looking at the IBM documentation, you can specify tasks to be carried out at the source or destination agents either before or after the copy, using presrc, postdst etc. This task could be an Ant task that does the renaming?

Upvotes: 0

crowne
crowne

Reputation: 8534

This works for me :

<?xml version="1.0"?>
<project name="so-copy-rename" default="build2">

    <property name="ant-contrib-jar" value="${user.home}/.ant/lib/ant-contrib-1.0b3.jar"/>

    <target name="setup" unless="ant-contrib.present">
        <echo>Getting ant-contrib</echo>
        <mkdir dir="${user.home}/.ant/lib"/>
        <!--
            Note: change this to a locally hosted maven repository manager such as nexus http://nexus.sonatype.org/
        -->
        <get dest="${ant-contrib-jar}"
             src="http://repo1.maven.org/maven2/ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jar"/>  
    </target>

    <target name="taskdefs">
        <taskdef resource="net/sf/antcontrib/antlib.xml">
            <classpath>
                <pathelement location="${ant-contrib-jar}"/>
            </classpath>
        </taskdef>
    </target>

    <target name="build" depends="taskdefs">

        <property name="srcdir" value="src"/>
        <property name="targetdir" value="target"/>
        <property name="files" value="file1,file2,file3,file4"/>

        <mkdir dir="${targetdir}"/>

        <foreach list="${files}" target="copy-rename" param="srcfile" trim="true">

            <param name="srcdir" value="${srcdir}" />
            <param name="targetdir" value="${targetdir}" />

        </foreach>

    </target>

    <target name="copy-rename">

        <var name="src-suffix" value="txt"/>
        <var name="tgt-suffix" value="xls"/>

        <copy file="${srcdir}/${srcfile}.${src-suffix}" tofile="${targetdir}/${srcfile}.${tgt-suffix}" />

    </target>

<target name="build2" depends="taskdefs">

    <property name="srcdir" value="src"/>
    <property name="targetdir" value="target"/>

    <mkdir dir="${targetdir}"/>

    <foreach target="copy-rename2" param="srcfile">
        <path id="srcfilepath">
            <fileset dir="${srcdir}" casesensitive="yes">
                <include name="*.txt"/>
            </fileset>
        </path>

        <param name="targetdir" value="${targetdir}" />
    </foreach>


</target>

<target name="copy-rename2">

    <var name="basefile" value="" unset="true"/>
    <basename property="basefile" file="${srcfile}" suffix=".txt"/>

    <var name="tgt-suffix" value="xls"/>

    <copy file="${srcfile}" tofile="${targetdir}/${basefile}.${tgt-suffix}" />

</target>

</project>

Upvotes: 1

Related Questions