开发者

Copy newest file with filename filter in Ant

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

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">
        开发者_JS百科            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>

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)


Answer was:

<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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜