Nant - 获取最新文件夹

问题描述:

在没有编写自定义任务的情况下,是否有一个相对简单的方法来获取某个目录中最新文件夹的名称?递归不是必需的。我一直试图用directory :: get-creation-time和foreach循环来做到这一点,如果声明,yada yada。这太复杂了,我即将创建一个自定义任务。不过,我怀疑有一些简单的方法可以通过现有的nant特性来实现。Nant - 获取最新文件夹

我相信你是正确的说明这样做可能会造成混乱,特别是属性的工作方式。如果您不想编写自定义任务,则始终可以使用script task。例如:

<?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> 

这样,应该可以调用函数来获取最新的目录。实际功能的实现可以更改为任何东西(优化多一点或其他),但我已经包含了一个快速参考,以便如何使用脚本任务。它产生如下输出:

 
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.