Prem
Prem

Reputation: 628

How to check part of file name and delete/replace the file in ant?

How can I check part of file name from downloads and replace old file from target.
Version is part of file name. If old version file not exist then simply copy it. Consider files in 'downloads' are latest.

Folder 'downloads' have latest file from server. And 'target' folder where some file are already there.

File name pattern: <UNIQUE_NAME>_<VERSION>_<SOME-TYPE>.dat

/downloads - All are latest in here

FILE01_01.02.03_xy-z.dat
FILE02_02.03.04_xy-z.dat
FILE_11_03.04.05_xy-z.dat

/target - before
FILE02_01.00.02_xy-z.dat
FILE04_01.00.00_xy-z.dat
FILE_03_01.00.01_xy-z.dat
FILE_11_01.01.00_xy-z.dat

/target - after movement (ToDo)
FILE01_01.02.03_xy-z.dat - wasn't exist in target (simply copy)
FILE02_02.03.04_xy-z.dat - replaced by latest
FILE04_01.00.00_xy-z.dat - couldn't download latest (do nothing)
FILE_03_01.00.01_xy-z.dat - couldn't download latest (do nothing)
FILE_11_03.04.05_xy-z.dat - replaced by latest

Upvotes: 0

Views: 270

Answers (1)

Prem
Prem

Reputation: 628

Add library for 'for loop'

<taskdef resource="net/sf/antcontrib/antlib.xml">
    <classpath>
        <pathelement location="lib/ant-contrib-1.0b3.jar" />
    </classpath>
</taskdef>

Update by iterating all files in downloads

<target name="update" depends="download" description="moves downloded dat files to projects dest">
    <for param="file" delimiter=";">
        <path>
            <fileset dir="${downloads}" includes="*.dat" casesensitive="false" />
        </path>
        <sequential>
            <echo>file: @{file}</echo>
            <basename file="@{file}" property="filename" />
            <!--echo>filename: ${filename}</echo-->
            <propertyregex property="uniq" input="${filename}" regexp="^[^.]*(?=_)" select="\0" casesensitive="false" />
            <echo>uniq: ${uniq}</echo>
            <delete dir="${dest}" includes="${uniq}*" />
            <copy file="@{file}" tofile="${dest}/${filename}" />
            <var name="uniq" unset="true" />
            <var name="filename" unset="true" />
        </sequential>
    </for>
</target>

Upvotes: 0

Related Questions