在 Ant 中使用文件名过滤器复制最新文件 [英] Copy newest file with filename filter in Ant

查看:32
本文介绍了在 Ant 中使用文件名过滤器复制最新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Ant 从目录中复制最新文件,理想情况下我也希望对文件名进行过滤(例如:'*file.java'),但我不知道如何做到这一点

I'm trying to copy the newest file from a directory using Ant, ideally I'd like to have a filter on the filename too (eg: '*file.java'), but I don't have any idea of how to do this

到目前为止,我已经包含了我的脚本,但它在 JavaScript 中出现了问题,而且我没有足够的经验知道原因.

I've included my script so far, but it's breaking in the JavaScript and I've not got enough experience to know why.

<project name="test1" basedir=".">

    <property file="../local.properties" />
    <property file="../build.properties" />
    <property file="../default.properties" />

    <target name="init">        
        <copy todir=".">
            <fileset dir="/path/to/files">
                <scriptselector language="javascript">
                    var files = basedir.list();
                    var mostRecent = true;
                    for(var i = 0; i < files.size(); i++) {
                        mostRecent = mostRecent && (filename >= files[i]);
                    }
                    self.setSelected(mostRecent);
                </scriptselector>
            </fileset>
        </copy>
    </target>
</project>

刚刚意识到我从上面的 JavaScript 代码中得到的网站声明此选择器是仅选择目录中的最新文件,其中每个文件的文件名都带有时间戳",因此我的示例将不起作用,因为它使用名称作为修改日期.这就引出了一个问题,即如何在 JavaScript(或任何其他与 Ant 一起使用的语言)中获取文件的修改日期

edit: just realised the site i got the above JavaScript code from states that this selector is "to select only the newest file of a directory where each file has a time stamp as its filename" so my example won't work because it's using the name as the modified date. This leads to the question of how to get the modified date of a file in JavaScript (or any other language which works with Ant)

推荐答案

答案是:

<target name="init">
     <copy todir="." flatten="true">
        <last>
            <sort>
                <date xmlns="antlib:org.apache.tools.ant.types.resources.comparators"/>
                <resources>
                    <fileset dir="/path/to/files/">
                        <include name="*sdk*" />
                    </fileset>
                </resources>
            </sort>
        </last>
    </copy>
</target>

从头到尾更改并添加过滤器

edit: changed first to last and added filter

这篇关于在 Ant 中使用文件名过滤器复制最新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆