开发者

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)?


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}" />


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜