代码之家  ›  专栏  ›  技术社区  ›  Benoit

南特:我可以提取路径中的最后一个目录吗?

  •  1
  • Benoit  · 技术社区  · 16 年前

    在nant中,我希望能够提取路径中目录的姓氏。
    例如,我们有路径“c:\my\proj\source\test.my\dll”

    我想通过那个路径并提取'test.my'dll'

    有什么方法可以轻松做到这一点吗?

    6 回复  |  直到 9 年前
        1
  •  0
  •   Steve K    16 年前

    script 任务。您可以用C或其他语言编写自定义代码,并返回一个可以分配给属性的值。

        2
  •  4
  •   yoroto    10 年前

    实际上,您可以使用现有的nant字符串函数。只是有点难看…

    ${string::substring(path, string::last-index-of(path, '\') + 1, string::get-length(path) - string::last-index-of(path, '\') - 1)}
    
        3
  •  2
  •   Paul'OS    12 年前

    可以找到路径的父目录,然后使用字符串替换查找要查找的文件夹:

    <property name="some.dir" value="c:\my_proj\source\test.my_dll" />
    <property name="some.dir.parent" value="${directory::get-parent-directory(some.dir)}" />
    <property name="directory" value="${string::replace(some.dir, some.dir.parent + '\', '') }" />
    
        4
  •  1
  •   Nikhil Gupta    11 年前

    您可能想尝试添加到nant 0.93中的新函数(尽管仍在夜间构建中)-

    directory::get-name(path)

    这将返回 path .

    参照 nant help

        5
  •  0
  •   dguaraglia    16 年前

    不,你需要为类似的事情编写一个自定义任务。

        6
  •  0
  •   Frank Rem    9 年前

    扩展到Steve K:

    <script language="C#" prefix="path" >
        <code>
            <![CDATA[
              [Function("get-dir-name")]
              public static string GetDirName(string path) {
                  return System.IO.Path.GetFileName(path);
              }
            ]]>
        </code>
     </script>
    
    <target name="build">
        <foreach item="Folder" in="." property="path">
            <echo message="${path::get-dir-name(path)}" />
        </foreach>
    </target>