How to search for a file in a directory and read its names and print out largest numbered file using Ant?

33 views Asked by At

I need to develop an ANT script that reads a directory content. The files in this directory are numbered along with its name. I have to find the largest numbered file in this directory and return its name and the number.

For example:

>> ls
patch-aaa-050.jar
patch-aaa-051.jar
patch-aaa-052.jar
patch-aaa-053.jar
patch-aaa-054.jar
patch-aaa-055.jar

I need to echo out: Latest patch is patch-aaa-055: Patched level: 55

Although this is a very easy task in Bash, PowerShell or Python. I am a bit clueless in ANT. I have checked out dirset Type and fileset Type. These types are able to list the directories but parsing the file names are overwhelming for me. The design is to run only with ANT, so I am limited to use that tool.

Thanks

1

There are 1 answers

0
Rebse On

Use resource collection path combined with basename and script task, f.e.:

<project>
 <path id="foo">
  <last>
    <sort>
      <fileset dir="C:/WKS/temp" includes="*.jar"/>
    </sort>
  </last>
 </path>

 <basename file="${toString:foo}" property="foobar"/>

 <script language="javascript">
   var a = project.getProperty('foobar').split('.')[0];
   var b = project.getProperty('foobar').split('-')[2].split('.')[0];
   print ('Latest patch is ' + a + ': Patched level: ' + b);
 </script>
</project>

There are also other possibilities for sort, see ant manual