Reputation: 39094
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
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
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
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
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
Reputation: 6028
No. You'll need to write a custom task for something like that.
Upvotes: 0