Description
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.
Usage
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.
Script
<!-- Initial target will be always called first to verify properties -->
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" InitialTargets="VerifyInputParameters">
<!-- Target verifies that the property is not empty (throws error) -->
<!-- and has trailing slash (adds one if no slash provided) -->
<Target Name="VerifyInputParameters">
<Error Condition="'$(ExternalPath)' == ''" Text="ExternalPath is empty" />
<CreateProperty Condition="!HasTrailingSlash('$(ExternalPath)')" Value="$(ExternalPath)\">
<Output TaskParameter="Value" PropertyName="ExternalPath" />
</CreateProperty>
</Target>
<!-- Here goes the rest of the project -->
</Project>
Notes
While HasTrailingSlash is not documented, it is supported both in MSBuild 2.0 and MSBuild 3.5. Undocumented status is apparently a documentation error.