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 |
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
The DeleGrid is open-source under the new BSD License; read the license for what you’re allowed to do.
You can download the source here: Download Source.
You can download the latest binary here: Download Binary.
The source is also accessible from Subversion at: http://jagregory.googlecode.com/svn/trunk/JAGregory.NAntTasks/ (using user jagregory-read-only)

Comments...
Nobody's said anything yet.
Post a comment...