Nims
Nims

Reputation: 41

Get file name from path Msbuild

<PropertyGroup>
        <fileName>$(FilePath.Substring($(FilePath.LastIndexOf('\'))))</fileName>    
</PropertyGroup>

I tried the above code. But am getting the file name including the last '\'. For eg. \Data.xml. I need only Data.xml. How can I get it?

Thank you...

Upvotes: 2

Views: 3643

Answers (2)

Drew Noakes
Drew Noakes

Reputation: 311315

Note that items have the %(Filename) and %(Extension) well known item metadata:

<ItemGroup>
    <Foo Include="@(Bar)" Filename="%(Filename)%(Extension)" />
</ItemGroup>

Upvotes: 1

stijn
stijn

Reputation: 35921

You could add another Substring call or so to strip the first character, but more convenient and less error-prone is to use the proper System.IO.Path function, see Property Functions:

<PropertyGroup>
    <fileName>$([System.IO.Path]::GetFileName('$(FilePath)'))</fileName>    
</PropertyGroup>

Upvotes: 12

Related Questions