Peter
Peter

Reputation: 123

Ant: How to select the latest modified file from a directory?

Assume I have a directory which contains several files with the same name prefix and a timestamp, e.g.

my-directory:
- file-0749
- file-1253
- file-2304

How can I tell ANT to select the latest modified file from my directory (in this case this would be file-2304)?

Upvotes: 12

Views: 7302

Answers (2)

lolung
lolung

Reputation: 461

Found a way without an additional library:

<copy todir="${tmp.last.modified.dir}">
    <last id="last.modified">
        <sort>
            <date />
            <fileset dir="${my.dir}" />
        </sort>
    </last>
</copy>
<echo message="last modified file in ${my.dir}: ${ant.refid:last.modified}" />

You can work directly with ant.refid:last.modified like the echo task does. Don't forget to delete tmp.last.modified.dir.

Upvotes: 0

Lesmana
Lesmana

Reputation: 27053

You can do that with the TimestampSelector task from ant-contrib.

<timestampselector property="latest.modified">
  <path>
    <fileset dir="${my-directory.dir}">
      <include name="file-*" />
    </fileset>
  </path>
</timestampselector>

<echo message="${latest.modified}" />

Upvotes: 18

Related Questions