0

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

Prem
  • 316
  • 1
  • 5
  • 23
  • If possible using by javascript that also fine. – Prem Apr 01 '19 at 09:37
  • FILE_11_03.04.05_xy-z.dat doesnt have the format __ – Alan Apr 01 '19 at 09:38
  • Thanks @Alan, Actually that's the constraint. Unique part of name can also contain '_'. – Prem Apr 01 '19 at 10:28
  • I think formula for getting unique name of file is: upto the char before underscore before version. Which means, char before (_) underscore before first (.) dot. As first dot will come in version and underscore before that is joiner of unique name and version. – Prem Apr 01 '19 at 10:33
  • This is the default behavior of Ant's `copy` task. An existing file in the destination dir will only be overwritten if it is older than the file in the source dir, unless you set the `overwrite` attribute to true, in which case all files will be overwritten regardless of their modification times. https://ant.apache.org/manual/Tasks/copy.html – CAustin Apr 01 '19 at 23:05
  • Thanks @CAustin, But file name in destination is not same. Filename itself contains version. Only some part of file name is unique, that's in bold. – Prem Apr 02 '19 at 03:40
  • Sorry, I didn't notice that part. Do you need to keep the file names as they are? This problem could be solved pretty easily by removing the version from everything. – CAustin Apr 02 '19 at 22:06

1 Answers1

0

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>
Prem
  • 316
  • 1
  • 5
  • 23