getfilename NAnt task

As part of my current quest to fully automate our build, I found my self needing the ability to copy a database backup from our remote server. The backup is in a folder along with several other backups, with a filename based on the date. I didn’t fancy trying to programmatically guess the filename, so I wrote an NAnt task to grab the newest file in a directory. Thanks to Richard Case for his overview of how to create a custom NAnt task.

The getfilename task simply gets the filename of a file in a directory, then pushes the name into the specified property. The filename to find can be based on the creation date, last modified date etc…

The attributes are as follows:

Attribute Description Required
in The directory to search Yes
property The property to push the filename into. Yes
searchPattern Wildcard search pattern for finding the file. No
of The file to get the filename of.

NewestFile - The most recently created file
OldestFile - The oldest file in the directory
FirstModifiedFile - The file with the oldest last modified date
LastModifiedFile - The most recently modified file
FirstFile - The first file in the directory, using default sorting
LastFile - The last file in the directory, using default sorting
No - Defaults to NewestFile

An example usage is:

<?xml version="1.0"?>
<project name="Example" default="run">
  <target name="run">
    <getfilename of="NewestFile" in="C:\path\to\backups" searchPattern="*.bak" property="filename" />
    <echo message="Filename: ${filename}" />
  </target>
</project>

Foreseeable usage situations revolve around anything where you’d need to get the last modified, or newest file in a directory; backups, database scripts etc…

Downloads

Note from Future James: This is long gone.