Sean
Sean

Reputation: 11

How to get the last part of $(MSBuildProjectDirectory)

I can't figure out how to get the last part of $(MSBuildProjectDirectory).

For example, if the value was "c:\development\projects\project_branch" then I want just the last part "project_branch".

How can I do this?

Upvotes: 1

Views: 3044

Answers (3)

cheerless bog
cheerless bog

Reputation: 934

In 4.0+ you can use Property Functions to do this in one line.

In this case for example $([System.IO.Path]::GetDirectoryName($(MSBuildProjectDirectory)))

or you could use a String function.

Upvotes: 4

Mike Gilmore
Mike Gilmore

Reputation: 554

If you're following best practice, then your project directory will have the same name as your project file. Therefore, you should be able to use:

$(MSBuildProjectName)

Upvotes: 0

mhanney
mhanney

Reputation: 465

<Project DefaultTargets="BuildAll" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <Target Name="GetMSBuildProjectLocalDirectory">
        <CreateItem Include="$(MSBuildProjectDirectory)">
            <Output ItemName="MSBuildProjectDirectoryMeta" TaskParameter="Include"/>
        </CreateItem>
        <CreateProperty Value="%(MSBuildProjectDirectoryMeta.Filename)">
            <Output PropertyName="LocalDirectory" TaskParameter="Value"/>
        </CreateProperty>
    </Target>

    <Target Name="BuildAll" DependsOnTargets="GetMSBuildProjectLocalDirectory">
        <Message Text="$(LocalDirectory)" />
    </Target>

</Project>

Upvotes: 2

Related Questions