title: powershell Reading & writing to multiple files
body: <p>I have a text file that is an output log from a series of Oracle SQL scripts that are ran as part of a database deployment.
  I want to convert this to HTML so that any errors/warnings that I define in regular expressions can be made to stand out, as this log is several MB in size.</p>
  
  <p>This is all run on Windows, so I thought I’d try Powershell to do it so I’d learn something as well. I am using Powershell version 2.0 for all of this.</p>
  
  <p>So far I have the following code: </p>
  
  <pre><code>($ifile=input file, $ofile=output file)
  param($ifile, $ofile)
  
  Set-Content $ofile `&lt;html`&gt;
  Add-Content $ofile `&lt;body`&gt;
  
  (Get-Content $ifile) | 
      Foreach-Object {$_ -replace "&amp;","&amp;amp" `
          -replace "&gt;", "&amp;gt;" `
          -replace "&lt;", "&amp;lt;" `
          -replace '\b(?!(ORA-20+|ORA-01430|ORA-00955|ORA-02260|ORA-01442|ORA-01418|ORA-01408|ORA-31003)(?=\d|\b))^ORA-[0-9]+\d?\b(.*)' , '&lt;font color=red&gt;&lt;b&gt;ORAERROR-$_&lt;/b&gt;&lt;/font&gt;' `
          -replace '((?i)PLS-+|SP2-+)(.*)', '&lt;font color=red&gt;&lt;b&gt;ORAERROR-$_&lt;/b&gt;&lt;/font&gt;' `
          -replace '((?i)ORA-20+|ORA-01430|ORA-00955|ORA-02260|ORA-01442|ORA-01418|ORA-01408|ORA-31003)(.*)','&lt;font color=orange&gt;&lt;b&gt;ORAWARNING-$_&lt;/b&gt;&lt;/font&gt;'} | 
          foreach-object {$_ + "&lt;br&gt;"} | 
              Add-Content $ofile
  
  Add-Content $ofile `&lt;`/body`&gt;
  Add-Content $ofile `&lt;`/html`&gt;
  </code></pre>
  
  <p>Now I want to expand it to do more and look better and I’m stuck….</p>
  
  <p>Here is what I want to do:</p>
  
  <ul>
  <li>Output html &amp; body tags to output file</li>
  <li>Output a header to output file</li>
  <li>For each line:
  <ul>
  <li>replace any matched regular expressions with <b>line of matched text</b> </li>
  <li>If there was a match, output   (pointing to the id above) to outputfile</li>
  <li>output the line (matched or not) into a temp file with a &lt;br> at the end
  until EOF</li>
  <li>cat temp file onto end of output file.</li>
  </ul></li>
  </ul>
  
  <p>By doing this I hope to end up with an output file that looks something like this:</p>
  
  <pre><code>HEADER
  ---------------------------------
  Link to error $countnumber
  Link to error $countnumber
  Link to error $countnumber
  ..
  ..
  ..
  Link to error $countnumber N
  ----------------------------------------
  The whole of the log file including tags
  ..
  ..
  ..
  EOF
  </code></pre>
  
  <p>I'm specifically stuck around trying to do the -replace bits into the temp file but then also doing the &lt;a href...> bits to the outputfile at the same time. It may not even be possible the way I am approaching it!!</p>
  
  <p>Any help would be much appreciated.</p>
  
  <p>If I am way off course with my logic and understanding of Powershell, please can you point me in the general direction and I'll go and do some more reading.....lots of reading.</p>
  
  <p>Cheers
  Nick</p>