1

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sean
  • 11
  • 1
  • 2
  • What are you trying to use this value for? – Joseph Yaduvanshi Apr 20 '10 at 20:57
  • Want to deploy binaries for branches to a location that organizes the folders by branch name. So the closest thing without referring to subversion seems to be the folder name where our msbuild file is. There is a deploy task we use for automated nightly build, so the objective is to add a deploy task for branch. – Sean Apr 21 '10 at 13:03

3 Answers3

4

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.

Vimes
  • 10,577
  • 17
  • 66
  • 86
cheerless bog
  • 914
  • 8
  • 11
2
<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>
mhanney
  • 465
  • 2
  • 11
0

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)
Mike Gilmore
  • 544
  • 5
  • 4