Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSBuild: How to get the relative path to a file, given an absolute one?

Tags:

c#

.net

msbuild

I have a situation where I have a bunch of absolute paths, and I would like to convert them to relative paths based on another directory with MSBuild. Here is the code I have so far:

<PropertyGroup>
    <FromPath>$(Bar)</FromPath>
</PropertyGroup>
<ItemGroup>
    <AbsolutePaths Include="@(Foo)" Exclude="@(Baz)" />
    <PathsRelativeToBar Include="@(AbsolutePaths->'???')" /> <!-- What goes here? -->
</ItemGroup>

Any help would be appreciated, thanks!

edit: I found a C#-based solution in this StackOverflow question, but I'm not sure how (or if it's possible) to convert it to MSBuild.

like image 272
James Ko Avatar asked Feb 18 '16 03:02

James Ko


1 Answers

There is a native function in MSBuild called "MakeRelative"

Here is how you can use it.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="4.5" >
    <PropertyGroup>
        <Bar>c:\temp\msbuild-sample\2</Bar>
        <FromPath>$(Bar)</FromPath>
    </PropertyGroup>
    <ItemGroup>
        <AbsolutePaths Include="c:\temp\msbuild-sample\1\**\*.txt" />
    </ItemGroup>
    <Target Name="Build">
        <ItemGroup>
            <PathsRelativeToBar Include="@(AbsolutePaths)">
                <!-- Here is the magic... we're adding a piece of metadata with the relative path -->
                <RelativePath>$([MSBuild]::MakeRelative($(FromPath), %(AbsolutePaths.FullPath)))</RelativePath>
            </PathsRelativeToBar>
        </ItemGroup>
        <Message Text="----- Absolute paths -----" />
        <Message Text="%(AbsolutePaths.FullPath)" />
        <Message Text="----- Relative paths (showing full path) -----" />
        <Message Text="%(PathsRelativeToBar.FullPath)" />
        <Message Text="----- Relative paths (relative to $(FromPath)) -----" />
        <Message Text="%(PathsRelativeToBar.RelativePath)" />
    </Target>
</Project>

Here is a quick view of my current environment

C:\temp\msbuild-sample>dir /s /b

C:\temp\msbuild-sample\1
C:\temp\msbuild-sample\sample.build
C:\temp\msbuild-sample\1\1.1
C:\temp\msbuild-sample\1\1.txt
C:\temp\msbuild-sample\1\2.txt
C:\temp\msbuild-sample\1\1.1\3.txt
C:\temp\msbuild-sample\1\1.1\4.txt

and here is the output.

----- Absolute paths -----
c:\temp\msbuild-sample\1\1.1\3.txt
c:\temp\msbuild-sample\1\1.1\4.txt
c:\temp\msbuild-sample\1\1.txt
c:\temp\msbuild-sample\1\2.txt
----- Relative paths (showing full path) -----
c:\temp\msbuild-sample\1\1.1\3.txt
c:\temp\msbuild-sample\1\1.1\4.txt
c:\temp\msbuild-sample\1\1.txt
c:\temp\msbuild-sample\1\2.txt
----- Relative paths (relative to c:\temp\msbuild-sample\2) -----
..\1\1.1\3.txt
..\1\1.1\4.txt
..\1\1.txt
..\1\2.txt

Hope this helps :)

like image 120
duncanc Avatar answered Sep 21 '22 18:09

duncanc