title: Insert new line with text after n lines and 3 lines at beginning from other file - Windows
body: <p><br/></p>
  
  <p>Could you please help find a <strong>Windows</strong> solution that can solve the below needs?<br/></p>
  
  <p>Currently I do this manually in Notepad++ with regular expression, but this routine will be used by several persons and an automatic way will be better.</p>
  
  <p>I have a huge text file containing insert statements which will be loaded in SQL Server. <br/>What I'm trying to do is:<br/>
  <strong>1</strong> - insert BEGIN TRANSACTION; at the beginning of the file <br/>
  <strong>2</strong> - insert COMMIT TRANSACTION; at the END of the file <br/>
  <strong>3</strong> - insert 3 new lines with text after every 1000 lines: <br/>
          COMMIT TRANSACTION;    <br/>
          GO<br/>
          BEGIN TRANSACTION; <br/>
  <strong>4</strong> - Copy the first 3 line from another file to current file <br/></p>
  
  <p>This thing will happen in <strong>dynamic paths</strong>.<br/></p>
  
  <p>Here is an example for a file with 5000 lines: <br/></p>
  
  <p><strong>Input</strong></p>
  
  <p>Line_No Text<br/>
  1             insert into... <br/>
  500           insert into... <br/>
  1000          insert into... <br/>
  1001          insert into... <br/>
  2000          insert into... <br/>
  2001          insert into... <br/>
  5000          insert into... <br/></p>
  
  <p><strong>Desired output <br/></strong></p>
  
  <p>Line_No Text<br/>
  1st  line with text from other file in same path <br/>
  2nd  line with text from other file in same path <br/>
  3rd  line with text from other file in same path <br/>
  BEGIN TRANSACTION; <br/>
  1             insert into... <br/>
  500           insert into... <br/>
  1000          insert into... <br/>
  1001          COMMIT TRANSACTION;     <br/>
  1002          GO <br/>
  1003          BEGIN TRANSACTION; <br/>
  1004          insert into... <br/>
  2000          insert into... <br/>
  2001          COMMIT TRANSACTION;     <br/>
  2002          GO <br/>
  2003          BEGIN TRANSACTION; <br/>
  2004          insert into... <br/>
  5000          insert into... <br/>
  5001          COMMIT TRANSACTION;   <br/>
  <br/>
  Thank you so much! :)</p>
  
  <p>UPDATE:
  Here is the solution for doing this task using the solution @mjolinor has provided:</p>
  
  <pre><code>$cd = Get-Location
  $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
  $newfile = "$cd\newfile.sql"
  $oldfile = "$cd\oldfile.sql"
  $string = Get-Content $cd\otherfile.txt
  
  $header = @"
  :out $PSScriptRoot\log.txt
  USE $string
  GO
  BEGIN TRANSACTION; 
  "@
  
  $insert = @'
  COMMIT TRANSACTION; 
  GO
  BEGIN TRANSACTION; 
  '@
  
  $header | Set-Content $newfile
   Get-Content $oldfile -ReadCount 1000 |
     ForEach-Object { $_,$insert | Add-Content $newfile -Force}
   'COMMIT TRANSACTION;' | Add-Content $newfile
  </code></pre>
  
  <p>Thank you @mjolinor :-) !</p>