Nant - get a new folder

Is there a relatively easy way in nant without writing a custom task to get the name of the newest folder in a specific directory? Recursion is not needed. I tried to do this using the :: get-creation-time directory and foreach loop, as well as for yada yada statements. This is too complicated, and I'm going to create a custom task. However, I suspect there is an easier way to do this using existing nant functions.

+4
source share
1 answer

I believe that you are right in saying that doing this with a pure nant mode can be messy, especially in the way properties work in nant. If you do not want to write a custom task, you can always use a script task. For instance:

<?xml version="1.0"?> <project name="testing" basedir="."> <script language="C#" prefix="test" > <code> <![CDATA[ [Function("find-newest-dir")] public static string FindNewestDir( string startDir ) { string theNewestDir = string.Empty; DateTime theCreateTime = new DateTime(); DateTime theLastCreateTime = new DateTime(); string[] theDirs = Directory.GetDirectories( startDir ); for ( int theCurrentIdx = 0; theCurrentIdx < theDirs.Length; ++theCurrentIdx ) { if ( theCurrentIdx != 0 ) { DateTime theCurrentDirCreateTime = Directory.GetCreationTime( theDirs[ theCurrentIdx ] ); if ( theCurrentDirCreateTime >= theCreateTime ) { theNewestDir = theDirs[ theCurrentIdx ]; theCreateTime = theCurrentDirCreateTime; } } else { theNewestDir = theDirs[ theCurrentIdx ]; theCreateTime = Directory.GetCreationTime( theDirs[ theCurrentIdx ] ); } } return theNewestDir; } ]]> </code> </script> <property name="dir" value="" overwrite="false"/> <echo message="The newest directory is: ${test::find-newest-dir( dir )}"/> </project> 

With this, you should be able to call the function to get the latest catalog. The implementation of the actual function can be modified to be anything (optimized a bit more or something else), but I included a quick one for reference on how to use the script task. It produces the output as follows:

  nant -D: dir = c: \

 NAnt 0.85 (Build 0.85.2478.0; release; 10/14/2006)
 Copyright (C) 2001-2006 Gerry Shaw
 http://nant.sourceforge.net

 Buildfile: file: /// C: /tmp/NAnt.build
 Target framework: Microsoft .NET Framework 2.0

    [script] Scanning assembly "jdrgmbuy" for extensions.
      [echo] The newest directory is: C: \ tmp

 BUILD SUCCEEDED

 Total time: 0.3 seconds.
+6
source

All Articles