在没有编写自定义任务的情况下,是否有一种相对简单的方法来获取某个目录中最新文件夹的名称?不需要递归.我一直在尝试使用directory :: get-creation-time和foreach循环以及if语句,yada yada.它太复杂了,我打算创建一个自定义任务.但是,我怀疑有一些更简单的方法可以通过现有的nant功能来实现.
我相信你是正确的说明,以纯粹的方式做这件事可能会造成混乱,特别是财产的运作方式.如果您不想编写自定义任务,则始终可以使用脚本任务.例如:
<?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>
Run Code Online (Sandbox Code Playgroud)
有了这个,人们应该能够调用该函数来获取最新的目录.实际功能的实现可以改为任何东西(优化一点或更多),但我已经包括一个快速的参考如何使用脚本任务.它产生如下输出:
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.
| 归档时间: |
|
| 查看次数: |
1151 次 |
| 最近记录: |