20

I have a batch script that I want to call from an MSBuild project, and the documentation says I can't use output from the batch (either console / environment variables) in the MSBuild project.

Is there a workaround?

ripper234
  • 222,824
  • 274
  • 634
  • 905

1 Answers1

26

You can redirect the output of the command to a file using "> output.txt" and read that into a variable.

<PropertyGroup>
   <OutputFile>$(DropLocation)\$(BuildNumber)\Output.txt</OutputFile>
</PropertyGroup>
<Exec Command="dir > &quot;$(OutputFile)&quot;" />
<ReadLinesFromFile File="$(OutputFile)">
   <Output TaskParameter="Lines" ItemName="OutputLines"/>
</ReadLinesFromFile>
<Message Text="@(OutputLines->'%(Identity)', '%0a%0d')" />
Dave
  • 276
  • 4
  • 3
  • 5
    this looks very hacky, why should a file be written if the output of the command is just useful for the MSBuild logic? Then you have to delete the file later... – knocte Jan 20 '12 at 18:50
  • 1
    Builds that use logic like this become painful to manage but I'd rather it be working and ugly than not working at all. – evanmcdonnal Apr 13 '12 at 22:28
  • 2
    While I agree it is a bit sloppy, it is not that big of a deal to add the MSBuild line immediately after checking its contents so there are no stay files lying around. – NightOwl888 Sep 25 '12 at 17:29
  • 5
    FYI. A modern way it is mentioned in [How get exec task output with msbuild](http://stackoverflow.com/questions/8938679/how-get-exec-task-output-with-msbuild) – Gonzalo Contento Aug 09 '16 at 14:17
  • @GonzaloContento probably wanted to link [this answer](https://stackoverflow.com/a/16804205/177710) directly – Oliver Nov 14 '22 at 11:29