开发者

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

开发者 https://www.devze.com 2023-02-21 17:36 出处:网络
Assume I have a directory which contains several files with the same name prefix and a 开发者_运维百科timestamp, e.g.

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

精彩评论

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