Amin
Amin

Reputation: 41

How to check if a file exist and is empty in msbuild

I want to check if a file exist and is not empty in msbuild. How do I do that

Upvotes: 4

Views: 1070

Answers (1)

Michael Baker
Michael Baker

Reputation: 3434

You can use Exists and ReadAllText to get the contents of a file.

  <Target Name="Build">  
    <PropertyGroup>
      <TheFile>C:\Windows\System32\notepad.exe</TheFile>
      <FileContents Condition="Exists($(TheFile))">$([System.IO.File]::ReadAllText('C:\\Windows\System32\notepad.exe'))</FileContents>
    </PropertyGroup>
    <Message Condition="'$(FileContents)' != ''" Text="The file is not empty $(FileContents)" />
  </Target>

Upvotes: 2

Related Questions