Benoit
Benoit

Reputation: 39094

nant: Can I extract the last directory in a path?

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?

Upvotes: 1

Views: 1438

Answers (6)

Frank Rem
Frank Rem

Reputation: 3738

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>

Upvotes: 0

yoroto
yoroto

Reputation: 107

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)}

Upvotes: 5

Nikhil Gupta
Nikhil Gupta

Reputation: 1778

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

Upvotes: 1

Paul&#39;OS
Paul&#39;OS

Reputation: 21

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

Upvotes: 2

dguaraglia
dguaraglia

Reputation: 6028

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

Upvotes: 0

Steve K
Steve K

Reputation: 19596

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

Upvotes: 0

Related Questions