views:

148

answers:

1

Hi I have a visual studio project which includes postbuildevents in the following form:

MyTool.exe $(ProjectDir)somesrcfile.txt $(TargetDir)sometargetfile.bin

Now I want to add some logic saying that these steps are taking place only if the files have changed. In peudocode:

if (somesrcfile.txt is newer than sometargetfile.bin) { MyTool.exe $(ProjectDir)somesrcfile.txt $(TargetDir)sometargetfile.bin }

Can I do this with MsBuild?

EDIT: I just tried it with a simple copy command but it seems not to work. Also the message is not displayed when I build the solution.

<ItemGroup>
    <MyTextFile Include="*.txt" />
  </ItemGroup>

  <Target Name="Build" Inputs="@(MyTextFile)" Outputs="@(MyTextFile->'%(Filename).bin')">
      <CustomBuild>
        <Message>Encoding files...</Message>
        <Command>
            copy %(Identity) %(Filename).bin
        </Command>
        <Outputs>$(OutDir)%(Identity)</Outputs>
      </CustomBuild>
  </Target>
+3  A: 

Yes, it is possible by using the Inputs and Outputs attributes on your target.

See: How to: Build incrementally

In your case, it would look something like this:

  <Target Name="AfterBuild" DependsOnTargets="Test">
  </Target>

  <ItemGroup>
    <MyTextFile Include="*.txt" />
  </ItemGroup>

  <Target Name="Test" Inputs="@(MyTextFile)" Outputs="@(MyTextFile->'%(FileName).bin')">
    <Message Text="Copying @(MyTextFile)" Importance="high"/>

    <Copy SourceFiles="@(MyTextFile)"  DestinationFiles="@(MyTextFile->'%(FileName).bin')" />

  </Target>

This target will only run if input files are newer than output files.

Eric Eijkelenboom
Thank for reply! I just tried it with a simple copy command but it seems not to work. Also the message is not displayed when I build the solution. See my edits.
codymanix
Hi codymanix. Please see my edits. I just tested this example with MSBuild 3.5 and it works like a charm. The text files are only copied if they are newer than the bin files.
Eric Eijkelenboom
I now copied the example you have given in my csproj file directly insie the <Project> node at the end of the file. Nothing happens. The message is not shown. I've set the MSBuild output in visual studio to "diagnostic", the name of the target doesn't appear here.
codymanix
Have you updated the txt files? Otherwise the target won't run :)Also, try running MSBuild from the command line to see a more detailed output. Go to your project folder and type: MSBuild.exe <project file name> /v:d /target:Build (MSBuild.exe is located in C:\Windows\Microsoft.NET\Framework\v3.5). Then, somewhere at the end of the output, you might see something like: 'Target "Test" skipped, because all output files are up-to-date compared to the input files'.
Eric Eijkelenboom
Did you put your edits after the <Import ...> element? If not move them under that.
Sayed Ibrahim Hashimi
That was it :) Thank you!
codymanix