How to convert NAnt function "path::combine(path1, path2)" to MSBuild?
Asked Answered
C

2

2

I need to convert the function "path::combine(path1, path2)". Please help me if you have some idea. Thank you!

Cornall answered 24/3, 2010 at 11:31 Comment(3)
It's true that the CombinePath Task is not listed in the summary page but only in the tree navigation under "MSBuild Task reference".Stentor
I see where you mean. It looks like MSBuild Team added it recently in the list (the alphabetical order shows that).Cornall
I updated my example with a workaround which should return the BasePath if an empty Path is passed to the CombinePath Task.Stentor
S
2

Use the CombinePath Task:

<Project DefaultTargets="DefaultTarget" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <MyBasePath>.\a\b</MyBasePath>
        <MySecondPath>c\d</MySecondPath>
    </PropertyGroup>

    <Target Name="Combine">
        <PropertyGroup>
            <MySecondPath Condition="$(MySecondPath)==''">.\</MySecondPath>
        </PropertyGroup>
        <CombinePath BasePath="$(MyBasePath)" Paths="$(MySecondPath)">
            <Output TaskParameter="CombinedPaths" PropertyName="CombineOutput" />
        </CombinePath>
    </Target>

    <Target Name="DefaultTarget" DependsOnTargets="Combine">
        <Message Text="Result from Combine is $(CombineOutput)" />
    </Target>

</Project>
Stentor answered 24/3, 2010 at 22:17 Comment(2)
Thank you! It's so strange because I look for the task in MSBuild Task Reference from msdn.microsoft.com/en-us/library/7z253716(v=VS.90).aspx. Microsoft doesn't list <CombinePath... > there. :(Cornall
I've tested I found that: When the second path is empty, CombinePath doesn't return the first path. So, we should note this small difference when compared with NAnt path::combine.Cornall
C
0

Updating this post for newer MsBuild versions. From MSBuild 4.0 and up, you can use property functions

$([System.IO.Path]::Combine($(Path1),$(Path2)))
Colubrine answered 23/8, 2015 at 10:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.