Question

In Nant, I would like to be able to extract the last name of the directory in a path.
For example, we have the path 'c:\my_proj\source\test.my_dll\'

I would like to pass in that path and extract 'test.my_dll'

Is there a way to easily do this?

Was it helpful?

Solution

See the script task. You can write custom code in C# or whatever, and return a value that you can assign to a property.

OTHER TIPS

You can actually do it with existing NAnt string functions. Just a bit ugly...

${string::substring(path, string::last-index-of(path, '\') + 1, string::get-length(path) - string::last-index-of(path, '\') - 1)}

It is possible to find the parent directory of your path and then use string replace to find the folder you're looking for:

<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 + '\', '') }" />

You may want to try the new function added to nant 0.93 (still in the nightly builds though) -

directory::get-name(path)

This would return the name of the directory mentioned in the path.

Refer to nant help

No. You'll need to write a custom task for something like that.

Expanding on 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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top