views:

22

answers:

1

I'm using a WriteLinesToFile to update a change log file (txt). It appends the text to the end of the file. Ideally, I'd like to be able to write the changes to the start of this file.

Is there a simple task (e.g. in the Community or Extension packs) that does this?

+2  A: 

I haven't seen something like that in the custom task pack.

You could cheat by using ReadLinesFromFile and WriteLinesToFile :

<PropertyGroup>
  <LogFile>log.txt</LogFile>
</PropertyGroup>

<ItemGroup>
  <Log Include="Line1"/>
  <Log Include="Line2"/>
</ItemGroup>

<Target Name="WriteFromStart">
  <ReadLinesFromFile File="$(LogFile)" Condition="Exists('log.txt')">
    <Output TaskParameter="Lines" ItemName="Log"/>
  </ReadLinesFromFile>

  <WriteLinesToFile File="$(LogFile)" 
                    Lines="@(Log)" 
                    Condition="@(Log) != '' And (@(Log) != '\r\n' Or @(Log) != '\n')"
                    Overwrite="true">
  </WriteLinesToFile>
</Target>

Or you could create a custom task.

madgnome
Is there any way to stop it stripping the blank lines (between log entries)?
dommer
Add a condition to WriteLinesToFile --> Condition="@(Log) != '' And (@(Log) != '\r\n' Or @(Log) != '\n')"
madgnome
I bit the bullet yesterday and wrote a custom task, but I might go back to this as I don't like maintaining stuff unless it absolutely necessary. Thanks.
dommer
@dommer: If you're doing a good bit of exotic stuff in this direction, I'd suggest considering a powershell inline task or psake
Ruben Bartelink