代码之家  ›  专栏  ›  技术社区  ›  Tim Scott

Nant-获取最新文件夹

  •  4
  • Tim Scott  · 技术社区  · 16 年前

    在Nant中,有没有一种相对简单的方法,不需要编写自定义任务,就可以获取某个目录中最新文件夹的名称?不需要递归。我一直在尝试使用directory::get creation time和foreach循环以及if语句yada yada。这太复杂了,我将创建一个自定义任务。但是,我怀疑通过现有的nant特性有一些更简单的方法来实现这一点。

    1 回复  |  直到 16 年前
        1
  •  6
  •   Scott Saad    16 年前

    我相信你说的对 纯净的 南特时尚可能会造成混乱,特别是在南特的物业工作方式。如果不想编写自定义任务,可以始终使用 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.