MSBuild:给定绝对路径,如何获取文件的相对路径?
MSBuild: How to get the relative path to a file, given an absolute one?
我遇到这样一种情况,我有一堆 absolute 路径,我想将它们转换为 relative paths based on MSBuild 的另一个目录。这是我到目前为止的代码:
<PropertyGroup>
<FromPath>$(Bar)</FromPath>
</PropertyGroup>
<ItemGroup>
<AbsolutePaths Include="@(Foo)" Exclude="@(Baz)" />
<PathsRelativeToBar Include="@(AbsolutePaths->'???')" /> <!-- What goes here? -->
</ItemGroup>
任何帮助将不胜感激,谢谢!
编辑: 我在 this Whosebug 问题中找到了一个基于 C# 的解决方案,但我不确定如何(或如果可能)将其转换为MSBuild.
MSBuild 中有一个名为 "MakeRelative"
的本机函数
这里是你如何使用它。
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="4.5" >
<PropertyGroup>
<Bar>c:\temp\msbuild-sample</Bar>
<FromPath>$(Bar)</FromPath>
</PropertyGroup>
<ItemGroup>
<AbsolutePaths Include="c:\temp\msbuild-sample\**\*.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>
这是我当前环境的快速视图
C:\temp\msbuild-sample>dir /s /b
C:\temp\msbuild-sample
C:\temp\msbuild-sample\sample.build
C:\temp\msbuild-sample.1
C:\temp\msbuild-sample.txt
C:\temp\msbuild-sample.txt
C:\temp\msbuild-sample.1.txt
C:\temp\msbuild-sample.1.txt
这是输出。
----- Absolute paths -----
c:\temp\msbuild-sample.1.txt
c:\temp\msbuild-sample.1.txt
c:\temp\msbuild-sample.txt
c:\temp\msbuild-sample.txt
----- Relative paths (showing full path) -----
c:\temp\msbuild-sample.1.txt
c:\temp\msbuild-sample.1.txt
c:\temp\msbuild-sample.txt
c:\temp\msbuild-sample.txt
----- Relative paths (relative to c:\temp\msbuild-sample) -----
...1.txt
...1.txt
...txt
...txt
希望这对您有所帮助:)
我遇到这样一种情况,我有一堆 absolute 路径,我想将它们转换为 relative paths based on MSBuild 的另一个目录。这是我到目前为止的代码:
<PropertyGroup>
<FromPath>$(Bar)</FromPath>
</PropertyGroup>
<ItemGroup>
<AbsolutePaths Include="@(Foo)" Exclude="@(Baz)" />
<PathsRelativeToBar Include="@(AbsolutePaths->'???')" /> <!-- What goes here? -->
</ItemGroup>
任何帮助将不胜感激,谢谢!
编辑: 我在 this Whosebug 问题中找到了一个基于 C# 的解决方案,但我不确定如何(或如果可能)将其转换为MSBuild.
MSBuild 中有一个名为 "MakeRelative"
的本机函数这里是你如何使用它。
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="4.5" >
<PropertyGroup>
<Bar>c:\temp\msbuild-sample</Bar>
<FromPath>$(Bar)</FromPath>
</PropertyGroup>
<ItemGroup>
<AbsolutePaths Include="c:\temp\msbuild-sample\**\*.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>
这是我当前环境的快速视图
C:\temp\msbuild-sample>dir /s /b
C:\temp\msbuild-sample
C:\temp\msbuild-sample\sample.build
C:\temp\msbuild-sample.1
C:\temp\msbuild-sample.txt
C:\temp\msbuild-sample.txt
C:\temp\msbuild-sample.1.txt
C:\temp\msbuild-sample.1.txt
这是输出。
----- Absolute paths -----
c:\temp\msbuild-sample.1.txt
c:\temp\msbuild-sample.1.txt
c:\temp\msbuild-sample.txt
c:\temp\msbuild-sample.txt
----- Relative paths (showing full path) -----
c:\temp\msbuild-sample.1.txt
c:\temp\msbuild-sample.1.txt
c:\temp\msbuild-sample.txt
c:\temp\msbuild-sample.txt
----- Relative paths (relative to c:\temp\msbuild-sample) -----
...1.txt
...1.txt
...txt
...txt
希望这对您有所帮助:)