简体   繁体   中英

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

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.

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM