EditDescription
Sometimes properties must be passed to MSBuild when running scripts from command line; that can be easily accomplished using /p command switch (
msbuild project.proj /p:Property=Value).
Frequently such external properties are paths, and always relevant question with the paths is whether they have that terminal slash.
To ascertain the “slashiness” of the path one may use
HasTrailingSlash function, built-in into MSBuild engine. The function takes single string parameter and returns
true, if the parameter ends with “\” or
false otherwise.
EditUsage
Best way to make sure that externally passed property has terminal slash is to create initial target to test for that condition (also it is recommended to check for empty properties and either to raise an error or provide default value).
The example in Script section demonstrates such initial target using
HasTrailingSlash function to check for terminal slash and
Error element to test for empty property.
EditScript
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" InitialTargets="VerifyInputParameters">
<Target Name="VerifyInputParameters">
<Error Condition="'$(ExternalPath)' == ''" Text="ExternalPath is empty" />
<CreateProperty Condition="!HasTrailingSlash('$(ExternalPath)')" Value="$(ExternalPath)\">
<Output TaskParameter="Value" PropertyName="ExternalPath" />
</CreateProperty>
</Target>
</Project>
EditNotes
While HasTrailingSlash is not documented, it is supported both in MSBuild 2.0 and MSBuild 3.5. Undocumented status is apparently a documentation error.